Display thumbprint rather than whole certificate in screen dialogue.
[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         _certificate_thumbprint = new wxStaticText (this, wxID_ANY, wxT (""));
44         wxFont font = _certificate_thumbprint->GetFont ();
45         font.SetFamily (wxFONTFAMILY_TELETYPE);
46         _certificate_thumbprint->SetFont (font);
47         if (certificate) {
48                 _certificate_thumbprint->SetLabel (std_to_wx (certificate->thumbprint ()));
49         }
50         _load_certificate = new wxButton (this, wxID_ANY, _("Load from file..."));
51         _download_certificate = new wxButton (this, wxID_ANY, _("Download..."));
52         s->Add (_certificate_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
53         s->Add (_load_certificate, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
54         s->Add (_download_certificate, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
55         add (s);
56
57         _load_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::select_certificate, this));
58         _download_certificate->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::download_certificate, this));
59
60         setup_sensitivity ();
61         layout ();
62 }
63
64 string
65 ScreenDialog::name () const
66 {
67         return wx_to_std (_name->GetValue());
68 }
69
70 optional<dcp::Certificate>
71 ScreenDialog::certificate () const
72 {
73         return _certificate;
74 }
75
76 void
77 ScreenDialog::load_certificate (boost::filesystem::path file)
78 {
79         try {
80                 _certificate = dcp::Certificate (dcp::file_to_string (file));
81                 _certificate_thumbprint->SetLabel (std_to_wx (_certificate->thumbprint ()));
82         } catch (dcp::MiscError& e) {
83                 error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), std_to_wx(e.what()).data()));
84         }
85 }
86
87 void
88 ScreenDialog::select_certificate ()
89 {
90         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
91         if (d->ShowModal () == wxID_OK) {
92                 load_certificate (boost::filesystem::path (wx_to_std (d->GetPath ())));
93         }
94         d->Destroy ();
95
96         setup_sensitivity ();
97 }
98
99 void
100 ScreenDialog::download_certificate ()
101 {
102         DownloadCertificateDialog* d = new DownloadCertificateDialog (this);
103         if (d->ShowModal() == wxID_OK) {
104                 _certificate = d->certificate ();
105                 _certificate_thumbprint->SetLabel (std_to_wx (_certificate->thumbprint ()));
106         }
107         d->Destroy ();
108         setup_sensitivity ();
109 }
110
111 void
112 ScreenDialog::setup_sensitivity ()
113 {
114         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
115         if (ok) {
116                 ok->Enable (static_cast<bool>(_certificate));
117         }
118 }