aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-browser.c
blob: 6b9d04f017d852db61023e3b2e08d4b0252556d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
/*
 * e-mail-browser.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-mail-browser.h"

#include <string.h>
#include <glib/gi18n.h>

#include "e-util/e-util.h"
#include "e-util/e-plugin-ui.h"
#include "shell/e-shell.h"
#include "shell/e-shell-utils.h"
#include "shell/e-shell-settings.h"
#include "widgets/misc/e-popup-action.h"
#include "widgets/misc/e-preview-pane.h"

#include "mail/e-mail-reader.h"
#include "mail/e-mail-reader-utils.h"
#include "mail/em-folder-tree-model.h"
#include "mail/message-list.h"

#define E_MAIL_BROWSER_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_BROWSER, EMailBrowserPrivate))

#define ACTION_GROUP_STANDARD       "action-group-standard"
#define ACTION_GROUP_SEARCH_FOLDERS "action-group-search-folders"

struct _EMailBrowserPrivate {
    EMailBackend *backend;
    GtkUIManager *ui_manager;
    EFocusTracker *focus_tracker;

    EMailFormatterMode mode;

    GtkWidget *main_menu;
    GtkWidget *main_toolbar;
    GtkWidget *message_list;
    GtkWidget *preview_pane;
    GtkWidget *statusbar;

    guint show_deleted : 1;
};

enum {
    PROP_0,
    PROP_BACKEND,
    PROP_FOCUS_TRACKER,
    PROP_FORWARD_STYLE,
    PROP_GROUP_BY_THREADS,
    PROP_SHOW_DELETED,
    PROP_REPLY_STYLE,
    PROP_UI_MANAGER,
    PROP_DISPLAY_MODE,
};

/* This is too trivial to put in a file.
 * It gets merged with the EMailReader UI. */
static const gchar *ui =
"<ui>"
"  <menubar name='main-menu'>"
"    <menu action='file-menu'>"
"      <placeholder name='file-actions'/>"
"      <placeholder name='print-actions'/>"
"      <separator/>"
"      <menuitem action='close'/>"
"    </menu>"
"    <menu action='edit-menu'>"
"      <placeholder name='selection-actions'>"
"        <menuitem action='cut-clipboard'/>"
"        <menuitem action='copy-clipboard'/>"
"        <menuitem action='paste-clipboard'/>"
"        <separator/>"
"        <menuitem action='select-all'/>"
"      </placeholder>"
"    </menu>"
"  </menubar>"
"</ui>";

static void e_mail_browser_reader_init
                    (EMailReaderInterface *interface);

G_DEFINE_TYPE_WITH_CODE (
    EMailBrowser,
    e_mail_browser,
    GTK_TYPE_WINDOW,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_MAIL_READER, e_mail_browser_reader_init)
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EXTENSIBLE, NULL))

static void
action_close_cb (GtkAction *action,
                 EMailBrowser *browser)
{
    e_mail_browser_close (browser);
}

static GtkActionEntry mail_browser_entries[] = {

    { "close",
      GTK_STOCK_CLOSE,
      NULL,
      NULL,
      N_("Close this window"),
      G_CALLBACK (action_close_cb) },

    { "copy-clipboard",
      GTK_STOCK_COPY,
      NULL,
      NULL,
      N_("Copy the selection"),
      NULL },  /* Handled by EFocusTracker */

    { "cut-clipboard",
      GTK_STOCK_CUT,
      NULL,
      NULL,
      N_("Cut the selection"),
      NULL },  /* Handled by EFocusTracker */

    { "paste-clipboard",
      GTK_STOCK_PASTE,
      NULL,
      NULL,
      N_("Paste the clipboard"),
      NULL },  /* Handled by EFocusTracker */

    { "select-all",
      GTK_STOCK_SELECT_ALL,
      NULL,
      NULL,
      N_("Select all text"),
      NULL },  /* Handled by EFocusTracker */

    /*** Menus ***/

    { "file-menu",
      NULL,
      N_("_File"),
      NULL,
      NULL,
      NULL },

    { "edit-menu",
      NULL,
      N_("_Edit"),
      NULL,
      NULL,
      NULL },

    { "view-menu",
      NULL,
      N_("_View"),
      NULL,
      NULL,
      NULL }
};

static EPopupActionEntry mail_browser_popup_entries[] = {

    { "popup-copy-clipboard",
      NULL,
      "copy-clipboard" }
};

static void
mail_browser_menu_item_select_cb (EMailBrowser *browser,
                                  GtkWidget *widget)
{
    GtkAction *action;
    GtkActivatable *activatable;
    GtkStatusbar *statusbar;
    const gchar *tooltip;
    guint context_id;
    gpointer data;

    activatable = GTK_ACTIVATABLE (widget);
    action = gtk_activatable_get_related_action (activatable);
    tooltip = gtk_action_get_tooltip (action);

    data = g_object_get_data (G_OBJECT (widget), "context-id");
    context_id = GPOINTER_TO_UINT (data);

    if (tooltip == NULL)
        return;

    statusbar = GTK_STATUSBAR (browser->priv->statusbar);
    gtk_statusbar_push (statusbar, context_id, tooltip);
}

static void
mail_browser_menu_item_deselect_cb (EMailBrowser *browser,
                                    GtkWidget *menu_item)
{
    GtkStatusbar *statusbar;
    guint context_id;
    gpointer data;

    data = g_object_get_data (G_OBJECT (menu_item), "context-id");
    context_id = GPOINTER_TO_UINT (data);

    statusbar = GTK_STATUSBAR (browser->priv->statusbar);
    gtk_statusbar_pop (statusbar, context_id);
}

static void
mail_browser_connect_proxy_cb (EMailBrowser *browser,
                               GtkAction *action,
                               GtkWidget *proxy)
{
    GtkStatusbar *statusbar;
    guint context_id;

    if (!GTK_IS_MENU_ITEM (proxy))
        return;

    statusbar = GTK_STATUSBAR (browser->priv->statusbar);
    context_id = gtk_statusbar_get_context_id (statusbar, G_STRFUNC);

    g_object_set_data (
        G_OBJECT (proxy), "context-id",
        GUINT_TO_POINTER (context_id));

    g_signal_connect_swapped (
        proxy, "select",
        G_CALLBACK (mail_browser_menu_item_select_cb), browser);

    g_signal_connect_swapped (
        proxy, "deselect",
        G_CALLBACK (mail_browser_menu_item_deselect_cb), browser);
}

static void
mail_browser_message_selected_cb (EMailBrowser *browser,
                                  const gchar *uid)
{
    CamelMessageInfo *info;
    CamelFolder *folder;
    EMailReader *reader;
    EMailDisplay *display;
    const gchar *title;
    guint32 state;

    reader = E_MAIL_READER (browser);
    state = e_mail_reader_check_state (reader);
    e_mail_reader_update_actions (reader, state);

    if (uid == NULL)
        return;

    folder = e_mail_reader_get_folder (reader);
    display = e_mail_reader_get_mail_display (reader);

    info = camel_folder_get_message_info (folder, uid);

    if (info == NULL)
        return;

    title = camel_message_info_subject (info);
    if (title == NULL || *title == '\0')
        title = _("(No Subject)");

    gtk_window_set_title (GTK_WINDOW (browser), title);
    gtk_widget_grab_focus (GTK_WIDGET (display));

    camel_message_info_set_flags (
        info, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
    camel_folder_free_message_info (folder, info);
}

static gboolean
close_on_idle_cb (gpointer browser)
{
    e_mail_browser_close (browser);
    return FALSE;
}

static void
mail_browser_message_list_built_cb (EMailBrowser *browser,
                                    MessageList *message_list)
{
    g_return_if_fail (E_IS_MAIL_BROWSER (browser));
    g_return_if_fail (IS_MESSAGE_LIST (message_list));

    if (!message_list_count (message_list))
        g_idle_add_full (G_PRIORITY_DEFAULT, close_on_idle_cb, browser, NULL);
}

static gboolean
mail_browser_popup_event_cb (EMailBrowser *browser,
                             GdkEventButton *event,
                             const gchar *uri)
{
    EMailReader *reader;
    EWebView *web_view;
    GtkMenu *menu;
    guint32 state;

    if (uri != NULL)
        return FALSE;

    reader = E_MAIL_READER (browser);
    web_view = E_WEB_VIEW (e_mail_reader_get_mail_display (reader));

    if (e_web_view_get_cursor_image (web_view) != NULL)
        return FALSE;

    menu = e_mail_reader_get_popup_menu (reader);

    state = e_mail_reader_check_state (reader);
    e_mail_reader_update_actions (reader, state);

    if (event == NULL)
        gtk_menu_popup (
            menu, NULL, NULL, NULL, NULL,
            0, gtk_get_current_event_time ());
    else
        gtk_menu_popup (
            menu, NULL, NULL, NULL, NULL,
            event->button, event->time);

    return TRUE;
}

static void
mail_browser_status_message_cb (EMailBrowser *browser,
                                const gchar *status_message)
{
    GtkStatusbar *statusbar;
    guint context_id;

    statusbar = GTK_STATUSBAR (browser->priv->statusbar);
    context_id = gtk_statusbar_get_context_id (statusbar, G_STRFUNC);

    /* Always pop first.  This prevents messages from piling up. */
    gtk_statusbar_pop (statusbar, context_id);

    if (status_message != NULL && *status_message != '\0')
        gtk_statusbar_push (statusbar, context_id, status_message);
}

static void
mail_browser_set_backend (EMailBrowser *browser,
                          EMailBackend *backend)
{
    g_return_if_fail (E_IS_MAIL_BACKEND (backend));
    g_return_if_fail (browser->priv->backend == NULL);

    browser->priv->backend = g_object_ref (backend);
}

static void
mail_browser_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BACKEND:
            mail_browser_set_backend (
                E_MAIL_BROWSER (object),
                g_value_get_object (value));
            return;

        case PROP_FORWARD_STYLE:
            e_mail_reader_set_forward_style (
                E_MAIL_READER (object),
                g_value_get_enum (value));
            return;

        case PROP_GROUP_BY_THREADS:
            e_mail_reader_set_group_by_threads (
                E_MAIL_READER (object),
                g_value_get_boolean (value));
            return;

        case PROP_REPLY_STYLE:
            e_mail_reader_set_reply_style (
                E_MAIL_READER (object),
                g_value_get_enum (value));
            return;

        case PROP_SHOW_DELETED:
            e_mail_browser_set_show_deleted (
                E_MAIL_BROWSER (object),
                g_value_get_boolean (value));
            return;

        case PROP_DISPLAY_MODE:
            E_MAIL_BROWSER (object)->priv->mode =
                g_value_get_int (value);
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_browser_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BACKEND:
            g_value_set_object (
                value, e_mail_reader_get_backend (
                E_MAIL_READER (object)));
            return;

        case PROP_FOCUS_TRACKER:
            g_value_set_object (
                value, e_mail_browser_get_focus_tracker (
                E_MAIL_BROWSER (object)));
            return;

        case PROP_FORWARD_STYLE:
            g_value_set_enum (
                value, e_mail_reader_get_forward_style (
                E_MAIL_READER (object)));
            return;

        case PROP_GROUP_BY_THREADS:
            g_value_set_boolean (
                value, e_mail_reader_get_group_by_threads (
                E_MAIL_READER (object)));
            return;

        case PROP_REPLY_STYLE:
            g_value_set_enum (
                value, e_mail_reader_get_reply_style (
                E_MAIL_READER (object)));
            return;

        case PROP_SHOW_DELETED:
            g_value_set_boolean (
                value, e_mail_browser_get_show_deleted (
                E_MAIL_BROWSER (object)));
            return;

        case PROP_UI_MANAGER:
            g_value_set_object (
                value, e_mail_browser_get_ui_manager (
                E_MAIL_BROWSER (object)));
            return;

        case PROP_DISPLAY_MODE:
            g_value_set_int (
                value, E_MAIL_BROWSER (object)->priv->mode);
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_browser_dispose (GObject *object)
{
    EMailBrowserPrivate *priv;

    priv = E_MAIL_BROWSER_GET_PRIVATE (object);

    if (priv->backend != NULL) {
        g_object_unref (priv->backend);
        priv->backend = NULL;
    }

    if (priv->ui_manager != NULL) {
        g_object_unref (priv->ui_manager);
        priv->ui_manager = NULL;
    }

    if (priv->focus_tracker != NULL) {
        g_object_unref (priv->focus_tracker);
        priv->focus_tracker = NULL;
    }

    if (priv->main_menu != NULL) {
        g_object_unref (priv->main_menu);
        priv->main_menu = NULL;
    }

    if (priv->main_toolbar != NULL) {
        g_object_unref (priv->main_toolbar);
        priv->main_toolbar = NULL;
    }

    if (priv->message_list != NULL) {
        /* This will cancel a regen operation. */
        gtk_widget_destroy (priv->message_list);
        priv->message_list = NULL;
    }

    if (priv->preview_pane != NULL) {
        g_object_unref (priv->preview_pane);
        priv->preview_pane = NULL;
    }

    if (priv->statusbar != NULL) {
        g_object_unref (priv->statusbar);
        priv->statusbar = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_browser_parent_class)->dispose (object);
}

static void
mail_browser_constructed (GObject *object)
{
    EMailBrowser *browser;
    EMailReader *reader;
    EMailBackend *backend;
    EMailSession *session;
    EMailDisplay *display;
    EShellBackend *shell_backend;
    EShell *shell;
    EFocusTracker *focus_tracker;
    GSettings *settings;
    GtkAccelGroup *accel_group;
    GtkActionGroup *action_group;
    GtkAction *action;
    GtkUIManager *ui_manager;
    GtkWidget *container;
    GtkWidget *widget;
    const gchar *domain;
    const gchar *id;
    guint merge_id;

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_browser_parent_class)->constructed (object);

    browser = E_MAIL_BROWSER (object);
    reader = E_MAIL_READER (object);
    backend = e_mail_reader_get_backend (reader);
    session = e_mail_backend_get_session (backend);

    shell_backend = E_SHELL_BACKEND (backend);
    shell = e_shell_backend_get_shell (shell_backend);

    ui_manager = e_ui_manager_new ();
    e_shell_configure_ui_manager (shell, E_UI_MANAGER (ui_manager));

    browser->priv->ui_manager = ui_manager;
    domain = GETTEXT_PACKAGE;

    gtk_application_add_window (
        GTK_APPLICATION (shell), GTK_WINDOW (object));

    /* The message list is a widget, but it is not shown in the browser.
     * Unfortunately, the widget is inseparable from its model, and the
     * model is all we need. */
    browser->priv->message_list = message_list_new (session);
    g_object_ref_sink (browser->priv->message_list);

    g_signal_connect_swapped (
        browser->priv->message_list, "message-selected",
        G_CALLBACK (mail_browser_message_selected_cb), object);

    g_signal_connect_swapped (
        browser->priv->message_list, "message-list-built",
        G_CALLBACK (mail_browser_message_list_built_cb), object);

    display = g_object_new (
        E_TYPE_MAIL_DISPLAY,
        "mode", E_MAIL_BROWSER (object)->priv->mode, NULL);

    g_signal_connect_swapped (
        display, "popup-event",
        G_CALLBACK (mail_browser_popup_event_cb), object);

    g_signal_connect_swapped (
        display, "status-message",
        G_CALLBACK (mail_browser_status_message_cb), object);

    widget = e_preview_pane_new (E_WEB_VIEW (display));
    browser->priv->preview_pane = g_object_ref (widget);
    gtk_widget_show (widget);

    action_group = gtk_action_group_new (ACTION_GROUP_STANDARD);
    gtk_action_group_set_translation_domain (action_group, domain);
    gtk_action_group_add_actions (
        action_group, mail_browser_entries,
        G_N_ELEMENTS (mail_browser_entries), object);
    e_action_group_add_popup_actions (
        action_group, mail_browser_popup_entries,
        G_N_ELEMENTS (mail_browser_popup_entries));
    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);

    /* For easy access.  Takes ownership of the reference. */

    g_object_set_data_full (
        object, ACTION_GROUP_STANDARD,
        action_group, (GDestroyNotify) g_object_unref);

    action_group = gtk_action_group_new (ACTION_GROUP_SEARCH_FOLDERS);
    gtk_action_group_set_translation_domain (action_group, domain);
    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);

    /* For easy access.  Takes ownership of the reference. */
    g_object_set_data_full (
        object, ACTION_GROUP_SEARCH_FOLDERS,
        action_group, (GDestroyNotify) g_object_unref);

    e_mail_reader_init (reader, TRUE, TRUE);

    e_ui_manager_add_ui_from_file (
        E_UI_MANAGER (ui_manager), E_MAIL_READER_UI_DEFINITION);
    e_ui_manager_add_ui_from_string (
        E_UI_MANAGER (ui_manager), ui, NULL);

    merge_id = gtk_ui_manager_new_merge_id (GTK_UI_MANAGER (ui_manager));
    e_mail_reader_create_charset_menu (reader, ui_manager, merge_id);

    accel_group = gtk_ui_manager_get_accel_group (ui_manager);
    gtk_window_add_accel_group (GTK_WINDOW (object), accel_group);

    g_signal_connect_swapped (
        ui_manager, "connect-proxy",
        G_CALLBACK (mail_browser_connect_proxy_cb), object);

    /* Configure an EFocusTracker to manage selection actions. */

    focus_tracker = e_focus_tracker_new (GTK_WINDOW (object));
    action = e_mail_reader_get_action (reader, "cut-clipboard");
    e_focus_tracker_set_cut_clipboard_action (focus_tracker, action);
    action = e_mail_reader_get_action (reader, "copy-clipboard");
    e_focus_tracker_set_copy_clipboard_action (focus_tracker, action);
    action = e_mail_reader_get_action (reader, "paste-clipboard");
    e_focus_tracker_set_paste_clipboard_action (focus_tracker, action);
    action = e_mail_reader_get_action (reader, "select-all");
    e_focus_tracker_set_select_all_action (focus_tracker, action);
    browser->priv->focus_tracker = focus_tracker;

    /* Construct window widgets. */

    widget = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (object), widget);
    gtk_widget_show (widget);

    container = widget;

    widget = gtk_statusbar_new ();
    gtk_box_pack_end (GTK_BOX (container), widget, FALSE, FALSE, 0);
    browser->priv->statusbar = g_object_ref (widget);
    gtk_widget_show (widget);

    widget = gtk_ui_manager_get_widget (ui_manager, "/main-menu");
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    browser->priv->main_menu = g_object_ref (widget);
    gtk_widget_show (widget);

    widget = gtk_ui_manager_get_widget (ui_manager, "/main-toolbar");
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    browser->priv->main_toolbar = g_object_ref (widget);
    gtk_widget_show (widget);

    gtk_style_context_add_class (
        gtk_widget_get_style_context (widget),
        GTK_STYLE_CLASS_PRIMARY_TOOLBAR);

    gtk_container_add (GTK_CONTAINER (container), browser->priv->preview_pane);

    /* Bind GObject properties to GSettings keys. */

    settings = g_settings_new ("org.gnome.evolution.mail");

    g_settings_bind (
        settings, "show-deleted",
        reader, "show-deleted",
        G_SETTINGS_BIND_DEFAULT);

    g_object_unref (settings);

    id = "org.gnome.evolution.mail.browser";
    e_plugin_ui_register_manager (ui_manager, id, object);
    e_plugin_ui_enable_manager (ui_manager, id);

    e_extensible_load_extensions (E_EXTENSIBLE (object));
}

static gboolean
mail_browser_key_press_event (GtkWidget *widget,
                              GdkEventKey *event)
{
    if (event->keyval == GDK_KEY_Escape) {
        e_mail_browser_close (E_MAIL_BROWSER (widget));
        return TRUE;
    }

    /* Chain up to parent's key_press_event() method. */
    return GTK_WIDGET_CLASS (e_mail_browser_parent_class)->
        key_press_event (widget, event);
}

static GtkActionGroup *
mail_browser_get_action_group (EMailReader *reader,
                               EMailReaderActionGroup group)
{
    const gchar *group_name;

    switch (group) {
        case E_MAIL_READER_ACTION_GROUP_STANDARD:
            group_name = ACTION_GROUP_STANDARD;
            break;
        case E_MAIL_READER_ACTION_GROUP_SEARCH_FOLDERS:
            group_name = ACTION_GROUP_SEARCH_FOLDERS;
            break;
        default:
            g_return_val_if_reached (NULL);
    }

    return g_object_get_data (G_OBJECT (reader), group_name);
}

static EMailBackend *
mail_browser_get_backend (EMailReader *reader)
{
    EMailBrowser *browser;

    browser = E_MAIL_BROWSER (reader);

    return browser->priv->backend;
}

static gboolean
mail_browser_get_hide_deleted (EMailReader *reader)
{
    EMailBrowser *browser;

    browser = E_MAIL_BROWSER (reader);

    return !e_mail_browser_get_show_deleted (browser);
}

static EMailDisplay *
mail_browser_get_mail_display (EMailReader *reader)
{
    EMailBrowserPrivate *priv;

    priv = E_MAIL_BROWSER_GET_PRIVATE (E_MAIL_BROWSER (reader));

    return E_MAIL_DISPLAY (e_preview_pane_get_web_view (
                    E_PREVIEW_PANE (priv->preview_pane)));
}

static GtkWidget *
mail_browser_get_message_list (EMailReader *reader)
{
    EMailBrowser *browser;

    browser = E_MAIL_BROWSER (reader);

    return browser->priv->message_list;
}

static GtkMenu *
mail_browser_get_popup_menu (EMailReader *reader)
{
    EMailBrowser *browser;
    GtkUIManager *ui_manager;
    GtkWidget *widget;

    browser = E_MAIL_BROWSER (reader);
    ui_manager = e_mail_browser_get_ui_manager (browser);
    widget = gtk_ui_manager_get_widget (ui_manager, "/mail-preview-popup");

    return GTK_MENU (widget);
}

static EPreviewPane *
mail_browser_get_preview_pane (EMailReader *reader)
{
    EMailBrowser *browser;

    browser = E_MAIL_BROWSER (reader);

    return E_PREVIEW_PANE (browser->priv->preview_pane);
}

static GtkWindow *
mail_browser_get_window (EMailReader *reader)
{
    return GTK_WINDOW (reader);
}

static void
mail_browser_set_message (EMailReader *reader,
                          const gchar *uid)
{
    EMailReaderInterface *interface;
    CamelMessageInfo *info;
    CamelFolder *folder;

    /* Chain up to parent's set_message() method. */
    interface = g_type_default_interface_peek (E_TYPE_MAIL_READER);
    interface->set_message (reader, uid);

    if (uid == NULL) {
        e_mail_browser_close (E_MAIL_BROWSER (reader));
        return;
    }

    folder = e_mail_reader_get_folder (reader);
    info = camel_folder_get_message_info (folder, uid);

    if (info != NULL) {
        gtk_window_set_title (
            GTK_WINDOW (reader),
            camel_message_info_subject (info));
        camel_folder_free_message_info (folder, info);
    }
}

static void
e_mail_browser_class_init (EMailBrowserClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;

    g_type_class_add_private (class, sizeof (EMailBrowserPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_browser_set_property;
    object_class->get_property = mail_browser_get_property;
    object_class->dispose = mail_browser_dispose;
    object_class->constructed = mail_browser_constructed;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->key_press_event = mail_browser_key_press_event;

    g_object_class_install_property (
        object_class,
        PROP_BACKEND,
        g_param_spec_object (
            "backend",
            "Mail Backend",
            "The mail backend",
            E_TYPE_MAIL_BACKEND,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_property (
        object_class,
        PROP_FOCUS_TRACKER,
        g_param_spec_object (
            "focus-tracker",
            "Focus Tracker",
            NULL,
            E_TYPE_FOCUS_TRACKER,
            G_PARAM_READABLE));

    /* Inherited from EMailReader */
    g_object_class_override_property (
        object_class,
        PROP_FORWARD_STYLE,
        "forward-style");

    /* Inherited from EMailReader */
    g_object_class_override_property (
        object_class,
        PROP_GROUP_BY_THREADS,
        "group-by-threads");

    /* Inherited from EMailReader */
    g_object_class_override_property (
        object_class,
        PROP_REPLY_STYLE,
        "reply-style");

    g_object_class_install_property (
        object_class,
        PROP_SHOW_DELETED,
        g_param_spec_boolean (
            "show-deleted",
            "Show Deleted",
            "Show deleted messages",
            FALSE,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_DISPLAY_MODE,
        g_param_spec_int (
            "display-mode",
            "Display Mode",
            NULL,
            0,
            G_MAXINT,
            E_MAIL_FORMATTER_MODE_NORMAL,
            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
e_mail_browser_reader_init (EMailReaderInterface *interface)
{
    interface->get_action_group = mail_browser_get_action_group;
    interface->get_backend = mail_browser_get_backend;
    interface->get_mail_display = mail_browser_get_mail_display;
    interface->get_hide_deleted = mail_browser_get_hide_deleted;
    interface->get_message_list = mail_browser_get_message_list;
    interface->get_popup_menu = mail_browser_get_popup_menu;
    interface->get_preview_pane = mail_browser_get_preview_pane;
    interface->get_window = mail_browser_get_window;
    interface->set_message = mail_browser_set_message;
}

static void
e_mail_browser_init (EMailBrowser *browser)
{
    browser->priv = E_MAIL_BROWSER_GET_PRIVATE (browser);

    gtk_window_set_title (GTK_WINDOW (browser), _("Evolution"));
    gtk_window_set_default_size (GTK_WINDOW (browser), 600, 400);

    e_restore_window (
        GTK_WINDOW (browser),
        "/org/gnome/evolution/mail/browser-window/",
        E_RESTORE_WINDOW_SIZE);
}

GtkWidget *
e_mail_browser_new (EMailBackend *backend,
                    CamelFolder *folder,
                    const gchar *msg_uid,
                    EMailFormatterMode mode)
{
    GtkWidget *widget;

    g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);

    widget= g_object_new (
        E_TYPE_MAIL_BROWSER,
        "backend", backend,
        "display-mode", mode,
        NULL);

    return widget;
}

void
e_mail_browser_close (EMailBrowser *browser)
{
    g_return_if_fail (E_IS_MAIL_BROWSER (browser));

    gtk_widget_destroy (GTK_WIDGET (browser));
}

gboolean
e_mail_browser_get_show_deleted (EMailBrowser *browser)
{
    g_return_val_if_fail (E_IS_MAIL_BROWSER (browser), FALSE);

    return browser->priv->show_deleted;
}

void
e_mail_browser_set_show_deleted (EMailBrowser *browser,
                                 gboolean show_deleted)
{
    g_return_if_fail (E_IS_MAIL_BROWSER (browser));

    if (browser->priv->show_deleted == show_deleted)
        return;

    browser->priv->show_deleted = show_deleted;

    g_object_notify (G_OBJECT (browser), "show-deleted");
}

EFocusTracker *
e_mail_browser_get_focus_tracker (EMailBrowser *browser)
{
    g_return_val_if_fail (E_IS_MAIL_BROWSER (browser), NULL);

    return browser->priv->focus_tracker;
}

GtkUIManager *
e_mail_browser_get_ui_manager (EMailBrowser *browser)
{
    g_return_val_if_fail (E_IS_MAIL_BROWSER (browser), NULL);

    return browser->priv->ui_manager;
}