Update for TableDialog constructor change.
[dcpomatic.git] / src / wx / screen_dialog.cc
1 /*
2     Copyright (C) 2012-2015 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 <dcp/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 #include <iostream>
30
31 using std::string;
32 using std::cout;
33 using boost::optional;
34
35 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, optional<dcp::Certificate> certificate)
36         : TableDialog (parent, std_to_wx (title), 2, 1, true)
37         , _certificate (certificate)
38 {
39         add (_("Name"), true);
40         _name = add (new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1)));
41
42         add (_("Server manufacturer"), true);
43         _manufacturer = add (new wxChoice (this, wxID_ANY));
44
45         add (_("Certificate"), true);
46         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
47         _load_certificate = new wxButton (this, wxID_ANY, _("Load from file..."));
48         _download_certificate = new wxButton (this, wxID_ANY, _("Download..."));
49         s->Add (_load_certificate, 1, wxEXPAND);
50         s->Add (_download_certificate, 1, wxEXPAND);
51         add (s);
52
53         add_spacer ();
54         _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
55         if (certificate) {
56                 _certificate_text->SetValue (certificate->certificate ());
57         }
58         wxFont font = wxSystemSettings::GetFont (wxSYS_ANSI_FIXED_FONT);
59         font.SetPointSize (font.GetPointSize() / 2);
60         _certificate_text->SetFont (font);
61         add (_certificate_text);
62
63         _manufacturer->Append (_("Unknown"));
64         _manufacturer->Append (_("Doremi"));
65         _manufacturer->Append (_("Dolby"));
66         _manufacturer->Append (_("Other"));
67         _manufacturer->SetSelection (0);
68
69         _load_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::select_certificate, this));
70         _download_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_certificate, this));
71         _manufacturer->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ScreenDialog::setup_sensitivity, this));
72
73         setup_sensitivity ();
74         layout ();
75 }
76
77 string
78 ScreenDialog::name () const
79 {
80         return wx_to_std (_name->GetValue());
81 }
82
83 optional<dcp::Certificate>
84 ScreenDialog::certificate () const
85 {
86         return _certificate;
87 }
88
89 void
90 ScreenDialog::load_certificate (boost::filesystem::path file)
91 {
92         try {
93                 _certificate = dcp::Certificate (dcp::file_to_string (file));
94                 _certificate_text->SetValue (_certificate->certificate ());
95         } catch (dcp::MiscError& e) {
96                 error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), std_to_wx(e.what()).data()));
97         }
98 }
99
100 void
101 ScreenDialog::select_certificate ()
102 {
103         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
104         if (d->ShowModal () == wxID_OK) {
105                 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
106         }
107         d->Destroy ();
108
109         setup_sensitivity ();
110 }
111
112 void
113 ScreenDialog::download_certificate ()
114 {
115         if (_manufacturer->GetStringSelection() == _("Doremi")) {
116                 DownloadCertificateDialog* d = new DoremiCertificateDialog (this, boost::bind (&ScreenDialog::load_certificate, this, _1));
117                 d->ShowModal ();
118                 d->Destroy ();
119         } else if (_manufacturer->GetStringSelection() == _("Dolby")) {
120                 DownloadCertificateDialog* d = new DolbyCertificateDialog (this, boost::bind (&ScreenDialog::load_certificate, this, _1));
121                 d->ShowModal ();
122                 d->Destroy ();
123         }
124
125         setup_sensitivity ();
126 }
127
128 void
129 ScreenDialog::setup_sensitivity ()
130 {
131         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
132         if (ok) {
133                 ok->Enable (static_cast<bool>(_certificate));
134         }
135
136         _download_certificate->Enable (
137                 _manufacturer->GetStringSelection() == _("Doremi") ||
138                 _manufacturer->GetStringSelection() == _("Dolby")
139                 );
140 }