Start of Dolby certificate download.
[dcpomatic.git] / src / wx / screen_dialog.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/filepicker.h>
21 #include <wx/validate.h>
22 #include <libdcp/exceptions.h>
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include "screen_dialog.h"
26 #include "wx_util.h"
27 #include "doremi_certificate_dialog.h"
28 #include "dolby_certificate_dialog.h"
29
30 using std::string;
31 using std::cout;
32 using boost::shared_ptr;
33
34 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, shared_ptr<libdcp::Certificate> certificate)
35         : wxDialog (parent, wxID_ANY, std_to_wx (title))
36         , _certificate (certificate)
37 {
38         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
39         table->AddGrowableCol (1, 1);
40
41         add_label_to_sizer (table, this, "Name", true);
42         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
43         table->Add (_name, 1, wxEXPAND);
44
45         add_label_to_sizer (table, this, "Server manufacturer", true);
46         _manufacturer = new wxChoice (this, wxID_ANY);
47         table->Add (_manufacturer, 1, wxEXPAND);
48
49         add_label_to_sizer (table, this, _("Certificate"), true);
50         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
51         _load_certificate = new wxButton (this, wxID_ANY, _("Load from file..."));
52         _download_certificate = new wxButton (this, wxID_ANY, _("Download"));
53         s->Add (_load_certificate, 1, wxEXPAND);
54         s->Add (_download_certificate, 1, wxEXPAND);
55         table->Add (s, 1, wxEXPAND);
56
57         table->AddSpacer (0);
58         _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
59         if (certificate) {
60                 _certificate_text->SetValue (certificate->certificate ());
61         }
62         wxFont font = wxSystemSettings::GetFont (wxSYS_ANSI_FIXED_FONT);
63         font.SetPointSize (font.GetPointSize() / 2);
64         _certificate_text->SetFont (font);
65         table->Add (_certificate_text, 1, wxEXPAND);
66
67         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
68         overall_sizer->Add (table, 1, wxEXPAND | wxALL, 6);
69         
70         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
71         if (buttons) {
72                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
73         }
74
75         SetSizer (overall_sizer);
76         overall_sizer->Layout ();
77         overall_sizer->SetSizeHints (this);
78
79         _manufacturer->Append (_("Unknown"));
80         _manufacturer->Append (_("Doremi"));
81         _manufacturer->Append (_("Dolby"));
82         _manufacturer->Append (_("Other"));
83         _manufacturer->SetSelection (0);
84
85         _load_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::select_certificate, this));
86         _download_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_certificate, this));
87         _manufacturer->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ScreenDialog::setup_sensitivity, this));
88
89         setup_sensitivity ();
90 }
91
92 string
93 ScreenDialog::name () const
94 {
95         return wx_to_std (_name->GetValue());
96 }
97
98 shared_ptr<libdcp::Certificate>
99 ScreenDialog::certificate () const
100 {
101         return _certificate;
102 }
103
104 void
105 ScreenDialog::load_certificate (boost::filesystem::path file)
106 {
107         try {
108                 _certificate.reset (new libdcp::Certificate (file));
109                 _certificate_text->SetValue (_certificate->certificate ());
110         } catch (libdcp::MiscError& e) {
111                 error_dialog (this, String::compose ("Could not read certificate file (%1)", e.what()));
112         }
113 }
114
115 void
116 ScreenDialog::select_certificate ()
117 {
118         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
119         if (d->ShowModal () == wxID_OK) {
120                 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
121         }
122         d->Destroy ();
123
124         setup_sensitivity ();
125 }
126
127 void
128 ScreenDialog::download_certificate ()
129 {
130         if (_manufacturer->GetStringSelection() == _("Doremi")) {
131                 DownloadCertificateDialog* d = new DoremiCertificateDialog (this, boost::bind (&ScreenDialog::load_certificate, this, _1));
132                 d->setup ();
133                 d->ShowModal ();
134                 d->Destroy ();
135         } else if (_manufacturer->GetStringSelection() == _("Dolby")) {
136                 DownloadCertificateDialog* d = new DolbyCertificateDialog (this, boost::bind (&ScreenDialog::load_certificate, this, _1));
137                 d->setup ();
138                 d->ShowModal ();
139                 d->Destroy ();
140         }
141 }
142
143 void
144 ScreenDialog::setup_sensitivity ()
145 {
146         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
147         ok->Enable (_certificate);
148
149         _download_certificate->Enable (
150                 _manufacturer->GetStringSelection() == _("Doremi") ||
151                 _manufacturer->GetStringSelection() == _("Dolby")
152                 );
153 }