summaryrefslogtreecommitdiff
path: root/src/wx/recipient_dialog.cc
blob: e78058f03db8327393ac01012d1076ec4ca80d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "dcpomatic_button.h"
#include "download_certificate_dialog.h"
#include "recipient_dialog.h"
#include "static_text.h"
#include "table_dialog.h"
#include "wx_util.h"
#include "lib/util.h"
#include <dcp/exceptions.h>
#include <dcp/certificate_chain.h>
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
#include <wx/filepicker.h>
#include <wx/validate.h>
LIBDCP_ENABLE_WARNINGS
#include <iostream>


using std::cout;
using std::string;
using std::vector;
using boost::bind;
using boost::optional;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif


static string
column (string s)
{
	return s;
}


RecipientDialog::RecipientDialog (
	wxWindow* parent, wxString title, string name, string notes, vector<string> emails, optional<dcp::Certificate> recipient
	)
	: wxDialog (parent, wxID_ANY, title)
	, _recipient (recipient)
{
	auto overall_sizer = new wxBoxSizer (wxVERTICAL);
	SetSizer (overall_sizer);

	_sizer = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
	int r = 0;

	add_label_to_sizer (_sizer, this, _("Name"), true, wxGBPosition (r, 0));
	_name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
	_sizer->Add (_name, wxGBPosition (r, 1));
	++r;

	add_label_to_sizer (_sizer, this, _("Notes"), true, wxGBPosition (r, 0));
	_notes = new wxTextCtrl (this, wxID_ANY, std_to_wx (notes), wxDefaultPosition, wxSize (320, -1));
	_sizer->Add (_notes, wxGBPosition (r, 1));
	++r;

	add_label_to_sizer (_sizer, this, _("Email addresses for KDM delivery"), false, wxGBPosition (r, 0), wxGBSpan (1, 2));
	++r;

	copy (emails.begin(), emails.end(), back_inserter (_emails));

	vector<EditableListColumn> columns;
	columns.push_back(EditableListColumn(_("Address"), 400, true));
	_email_list = new EditableList<string> (
		this,
		columns,
		bind(&RecipientDialog::get_emails, this),
		bind(&RecipientDialog::set_emails, this, _1),
		EditableList<string>::add_with_dialog<EmailDialog>,
		EditableList<string>::edit_with_dialog<EmailDialog>,
		bind(&column, _1),
		EditableListTitle::VISIBLE,
		EditableListButton::NEW | EditableListButton::EDIT | EditableListButton::REMOVE
		);

	_sizer->Add (_email_list, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND);
	++r;

        wxClientDC dc (this);
	auto font = _name->GetFont ();
	font.SetFamily (wxFONTFAMILY_TELETYPE);
	dc.SetFont (font);
	auto size = dc.GetTextExtent(char_to_wx("1234567890123456789012345678"));
	size.SetHeight (-1);

	add_label_to_sizer (_sizer, this, _("Recipient certificate"), true, wxGBPosition (r, 0));
	auto s = new wxBoxSizer (wxHORIZONTAL);
	_recipient_thumbprint = new StaticText(this, {}, wxDefaultPosition, size);
	_recipient_thumbprint->SetFont (font);
	set_recipient (recipient);
	_get_recipient_from_file = new Button (this, _("Get from file..."));
	s->Add (_recipient_thumbprint, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
	s->Add (_get_recipient_from_file, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
	_sizer->Add (s, wxGBPosition (r, 1));
	++r;

	_name->Bind (wxEVT_TEXT, boost::bind (&RecipientDialog::setup_sensitivity, this));
	_get_recipient_from_file->Bind (wxEVT_BUTTON, boost::bind (&RecipientDialog::get_recipient_from_file, this));

	overall_sizer->Add (_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);

	auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
	if (buttons) {
		overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
	}

	overall_sizer->Layout ();
	overall_sizer->SetSizeHints (this);

	setup_sensitivity ();
}


string
RecipientDialog::name () const
{
	return wx_to_std (_name->GetValue());
}


string
RecipientDialog::notes () const
{
	return wx_to_std (_notes->GetValue());
}


optional<dcp::Certificate>
RecipientDialog::recipient () const
{
	return _recipient;
}


void
RecipientDialog::load_recipient (boost::filesystem::path file)
{
	try {
		/* Load this as a chain, in case it is one, and then pick the leaf certificate */
		dcp::CertificateChain c (dcp::file_to_string (file));
		if (c.unordered().empty()) {
			error_dialog (this, _("Could not read certificate file."));
			return;
		}
		set_recipient (c.leaf ());
	} catch (dcp::MiscError& e) {
		error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
	}
}


void
RecipientDialog::get_recipient_from_file ()
{
	wxFileDialog dialog(this, _("Select Certificate File"));
	if (dialog.ShowModal() == wxID_OK) {
		load_recipient(boost::filesystem::path(wx_to_std(dialog.GetPath())));
	}

	setup_sensitivity ();
}


void
RecipientDialog::setup_sensitivity ()
{
	auto ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
	if (ok) {
		ok->Enable (static_cast<bool>(_recipient) && !_name->GetValue().IsEmpty());
	}
}


void
RecipientDialog::set_recipient (optional<dcp::Certificate> r)
{
	_recipient = r;

	if (_recipient) {
		_recipient_thumbprint->SetLabel (std_to_wx (_recipient->thumbprint ()));
		_sizer->Layout ();
	}
}


vector<string>
RecipientDialog::get_emails () const
{
	return _emails;
}


void
RecipientDialog::set_emails (vector<string> e)
{
	_emails = e;
}


vector<string>
RecipientDialog::emails () const
{
	return _emails;
}