955f25049d5ff7419dbe093c2b3b60ff1f83791a
[dcpomatic.git] / src / wx / download_certificate_dialog.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "dolby_doremi_certificate_panel.h"
22 #include "download_certificate_dialog.h"
23 #include "wx_util.h"
24
25 using boost::optional;
26
27 DownloadCertificateDialog::DownloadCertificateDialog (wxWindow* parent)
28         : wxDialog (parent, wxID_ANY, _("Download certificate"))
29 {
30         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
31
32         _notebook = new wxNotebook (this, wxID_ANY);
33         sizer->Add (_notebook, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
34
35         _pages.push_back (new DolbyDoremiCertificatePanel (_notebook, this));
36         _setup.push_back (false);
37         _notebook->AddPage (_pages.back(), _("Dolby / Doremi"), true);
38
39         _download = new wxButton (this, wxID_ANY, _("Download"));
40         sizer->Add (_download, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
41
42         _message = new wxStaticText (this, wxID_ANY, wxT (""));
43         sizer->Add (_message, 0, wxALL, DCPOMATIC_SIZER_GAP);
44         wxFont font = _message->GetFont();
45         font.SetStyle (wxFONTSTYLE_ITALIC);
46         font.SetPointSize (font.GetPointSize() - 1);
47         _message->SetFont (font);
48
49         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
50         if (buttons) {
51                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
52         }
53
54         SetSizerAndFit (sizer);
55
56         _notebook->Bind (wxEVT_NOTEBOOK_PAGE_CHANGED, &DownloadCertificateDialog::page_changed, this);
57         _download->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&DownloadCertificateDialog::download, this));
58         _download->Enable (false);
59
60         wxNotebookEvent ev;
61         page_changed (ev);
62 }
63
64 DownloadCertificateDialog::~DownloadCertificateDialog ()
65 {
66         _notebook->Unbind (wxEVT_NOTEBOOK_PAGE_CHANGED, &DownloadCertificateDialog::page_changed, this);
67 }
68
69 void
70 DownloadCertificateDialog::download ()
71 {
72         _pages[_notebook->GetSelection()]->download (_message);
73 }
74
75 dcp::Certificate
76 DownloadCertificateDialog::certificate () const
77 {
78         optional<dcp::Certificate> c = _pages[_notebook->GetSelection()]->certificate ();
79         DCPOMATIC_ASSERT (c);
80         return c.get ();
81 }
82
83 void
84 DownloadCertificateDialog::setup_sensitivity ()
85 {
86         DownloadCertificatePanel* p = _pages[_notebook->GetSelection()];
87         _download->Enable (p->ready_to_download ());
88         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
89         if (ok) {
90                 ok->Enable (static_cast<bool>(p->certificate ()));
91         }
92
93 }
94
95 void
96 DownloadCertificateDialog::page_changed (wxNotebookEvent &)
97 {
98         int const n = _notebook->GetSelection();
99         if (!_setup[n]) {
100                 _pages[n]->setup ();
101                 _setup[n] = true;
102         }
103
104         setup_sensitivity ();
105 }