Rearrange cerficate download UI a bit.
[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 "screen_dialog.h"
21 #include "wx_util.h"
22 #include "download_certificate_dialog.h"
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include <dcp/exceptions.h>
26 #include <wx/filepicker.h>
27 #include <wx/validate.h>
28 #include <iostream>
29
30 using std::string;
31 using std::cout;
32 using boost::optional;
33
34 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, optional<dcp::Certificate> certificate)
35         : TableDialog (parent, std_to_wx (title), 2, 1, true)
36         , _certificate (certificate)
37 {
38         add (_("Name"), true);
39         _name = add (new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1)));
40
41         add (_("Certificate"), true);
42         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
43         _load_certificate = new wxButton (this, wxID_ANY, _("Load from file..."));
44         _download_certificate = new wxButton (this, wxID_ANY, _("Download..."));
45         s->Add (_load_certificate, 1, wxEXPAND);
46         s->Add (_download_certificate, 1, wxEXPAND);
47         add (s);
48
49         add_spacer ();
50         _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
51         if (certificate) {
52                 _certificate_text->SetValue (certificate->certificate ());
53         }
54         wxFont font = wxSystemSettings::GetFont (wxSYS_ANSI_FIXED_FONT);
55         font.SetPointSize (font.GetPointSize() / 2);
56         _certificate_text->SetFont (font);
57         add (_certificate_text);
58
59         _load_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::select_certificate, this));
60         _download_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_certificate, this));
61
62         setup_sensitivity ();
63         layout ();
64 }
65
66 string
67 ScreenDialog::name () const
68 {
69         return wx_to_std (_name->GetValue());
70 }
71
72 optional<dcp::Certificate>
73 ScreenDialog::certificate () const
74 {
75         return _certificate;
76 }
77
78 void
79 ScreenDialog::load_certificate (boost::filesystem::path file)
80 {
81         try {
82                 _certificate = dcp::Certificate (dcp::file_to_string (file));
83                 _certificate_text->SetValue (std_to_wx (_certificate->certificate ()));
84         } catch (dcp::MiscError& e) {
85                 error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), std_to_wx(e.what()).data()));
86         }
87 }
88
89 void
90 ScreenDialog::select_certificate ()
91 {
92         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
93         if (d->ShowModal () == wxID_OK) {
94                 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
95         }
96         d->Destroy ();
97
98         setup_sensitivity ();
99 }
100
101 void
102 ScreenDialog::download_certificate ()
103 {
104         DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
105         if (d->ShowModal() == wxID_OK) {
106                 _certificate = d->certificate ();
107                 _certificate_text->SetValue (std_to_wx (_certificate->certificate ()));
108         }
109         d->Destroy ();
110         setup_sensitivity ();
111 }
112
113 void
114 ScreenDialog::setup_sensitivity ()
115 {
116         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
117         if (ok) {
118                 ok->Enable (static_cast<bool>(_certificate));
119         }
120 }