e6f4eca5ea5c60f6b7f85e699550c020ef0d1a14
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "confirm_kdm_email_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "invalid_certificate_period_dialog.h"
25 #include "kdm_cpl_panel.h"
26 #include "kdm_dialog.h"
27 #include "kdm_output_panel.h"
28 #include "kdm_timing_panel.h"
29 #include "screens_panel.h"
30 #include "static_text.h"
31 #include "wx_util.h"
32 #include "wx_variant.h"
33 #include "lib/cinema.h"
34 #include "lib/config.h"
35 #include "lib/film.h"
36 #include "lib/job_manager.h"
37 #include "lib/kdm_with_metadata.h"
38 #include "lib/kdm_util.h"
39 #include "lib/screen.h"
40 #include <libcxml/cxml.h>
41 #include <dcp/cpl.h>
42 #include <dcp/exceptions.h>
43 #include <dcp/warnings.h>
44 LIBDCP_DISABLE_WARNINGS
45 #include <wx/listctrl.h>
46 #include <wx/treectrl.h>
47 LIBDCP_ENABLE_WARNINGS
48
49
50 using std::exception;
51 using std::list;
52 using std::make_pair;
53 using std::map;
54 using std::pair;
55 using std::runtime_error;
56 using std::shared_ptr;
57 using std::string;
58 using std::vector;
59 using boost::bind;
60 using boost::optional;
61 #if BOOST_VERSION >= 106100
62 using namespace boost::placeholders;
63 #endif
64
65
66 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
67         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
68         , _film (film)
69 {
70         /* Main sizers */
71         auto horizontal = new wxBoxSizer (wxHORIZONTAL);
72         auto left = new wxBoxSizer (wxVERTICAL);
73         auto right = new wxBoxSizer (wxVERTICAL);
74
75         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
76         horizontal->Add (right, 1, wxEXPAND);
77
78         /* Font for sub-headings */
79         wxFont subheading_font (*wxNORMAL_FONT);
80         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
81
82         /* Sub-heading: Screens */
83         auto h = new StaticText (this, _("Screens"));
84         h->SetFont (subheading_font);
85         left->Add (h, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
86         _screens = new ScreensPanel (this);
87         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
88
89         /* Sub-heading: Timing */
90         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
91         h = new StaticText (this, S_("KDM|Timing"));
92         h->SetFont (subheading_font);
93         right->Add (h);
94         _timing = new KDMTimingPanel (this);
95         right->Add (_timing);
96
97         /* Sub-heading: CPL */
98         h = new StaticText (this, _("CPL"));
99         h->SetFont (subheading_font);
100         right->Add (h);
101
102         vector<CPLSummary> cpls;
103         for (auto const& i: film->cpls()) {
104                 if (i.encrypted) {
105                         cpls.push_back (i);
106                 }
107         }
108
109         _cpl = new KDMCPLPanel (this, cpls);
110         right->Add (_cpl, 0, wxEXPAND);
111
112         /* Sub-heading: Output */
113         h = new StaticText (this, _("Output"));
114         h->SetFont (subheading_font);
115         right->Add(h, 0, wxTOP, DCPOMATIC_SUBHEADING_TOP_PAD);
116         _output = new KDMOutputPanel (this);
117         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
118
119         _make = new Button (this, _("Make KDMs"));
120         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
121
122         /* Make an overall sizer to get a nice border */
123
124         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
125         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
126
127         /* Bind */
128
129         _screens->ScreensChanged.connect(boost::bind(&KDMDialog::screens_changed, this));
130         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
131         _make->Bind (wxEVT_BUTTON, boost::bind (&KDMDialog::make_clicked, this));
132         _cpl->Changed.connect(boost::bind(&KDMDialog::cpl_changed, this));
133
134         cpl_changed();
135         setup_sensitivity ();
136
137         SetSizer (overall_sizer);
138         overall_sizer->Layout ();
139         overall_sizer->SetSizeHints (this);
140 }
141
142
143 void
144 KDMDialog::screens_changed()
145 {
146         _timing->suggest_utc_offset(_screens->best_utc_offset());
147         setup_sensitivity();
148 }
149
150
151 void
152 KDMDialog::cpl_changed()
153 {
154         try {
155                 dcp::CPL cpl(_cpl->cpl());
156                 if (auto text = cpl.annotation_text()) {
157                         _output->set_annotation_text(*text);
158                 }
159         } catch (...) {}
160
161         setup_sensitivity();
162 }
163
164
165 void
166 KDMDialog::setup_sensitivity ()
167 {
168         _screens->setup_sensitivity ();
169         _output->setup_sensitivity ();
170         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
171 }
172
173
174 bool
175 KDMDialog::confirm_overwrite (boost::filesystem::path path)
176 {
177         return confirm_dialog (
178                 this,
179                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
180                 );
181 }
182
183
184 void
185 KDMDialog::make_clicked ()
186 {
187         auto film = _film.lock ();
188         DCPOMATIC_ASSERT (film);
189
190         list<KDMWithMetadataPtr> kdms;
191         try {
192                 /* Start off by enabling forensic marking for all */
193                 optional<int> for_audio;
194                 if (!_output->forensic_mark_audio()) {
195                         /* No forensic marking for audio */
196                         for_audio = 0;
197                 } else if (_output->forensic_mark_audio_up_to()) {
198                         /* Forensic mark up to this channel; disabled on channels greater than this */
199                         for_audio = _output->forensic_mark_audio_up_to();
200                 }
201
202                 vector<KDMCertificatePeriod> period_checks;
203
204                 std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm = [film, this](dcp::LocalTime begin, dcp::LocalTime end) {
205                         return film->make_kdm(_cpl->cpl(), begin, end);
206                 };
207
208                 for (auto i: _screens->screens()) {
209                         auto p = kdm_for_screen(make_kdm, i, _timing->from(), _timing->until(), _output->formulation(), !_output->forensic_mark_video(), for_audio, period_checks);
210                         if (p) {
211                                 kdms.push_back (p);
212                         }
213                 }
214
215                 if (
216                         find_if(
217                                 period_checks.begin(),
218                                 period_checks.end(),
219                                 [](KDMCertificatePeriod const& p) { return p.overlap != KDMCertificateOverlap::KDM_WITHIN_CERTIFICATE; }
220                                ) != period_checks.end()) {
221                         InvalidCertificatePeriodDialog dialog(this, period_checks);
222                         if (dialog.ShowModal() == wxID_CANCEL) {
223                                 return;
224                         }
225                 }
226
227         } catch (dcp::BadKDMDateError& e) {
228                 if (e.starts_too_early()) {
229                         error_dialog (this, _("The KDM start period is before (or close to) the start of the signing certificate's validity period.  Use a later start time for this KDM."));
230                 } else {
231                         error_dialog(
232                                 this,
233                                 variant::wx::insert_dcpomatic(
234                                         _("The KDM end period is after (or close to) the end of the signing certificates' validity "
235                                           "period.  Either use an earlier end time for this KDM or re-create your signing certificates "
236                                           "in the %s preferences window."))
237                                 );
238                 }
239                 return;
240         } catch (runtime_error& e) {
241                 error_dialog (this, std_to_wx(e.what()));
242                 return;
243         }
244
245         auto result = _output->make(kdms, film->dcp_name(), bind (&KDMDialog::confirm_overwrite, this, _1));
246         if (result.first) {
247                 JobManager::instance()->add (result.first);
248         }
249
250         if (result.second > 0) {
251                 /* XXX: proper plural form support in wxWidgets? */
252                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
253                 message_dialog (
254                         this,
255                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
256                         );
257         }
258 }