f01c47ffbba3ad86636295695b4a0f16b70adf9d
[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 "kdm_cpl_panel.h"
25 #include "kdm_dialog.h"
26 #include "kdm_output_panel.h"
27 #include "kdm_timing_panel.h"
28 #include "screens_panel.h"
29 #include "static_text.h"
30 #include "wx_util.h"
31 #include "lib/cinema.h"
32 #include "lib/config.h"
33 #include "lib/film.h"
34 #include "lib/job_manager.h"
35 #include "lib/kdm_with_metadata.h"
36 #include "lib/kdm_util.h"
37 #include "lib/screen.h"
38 #include <libcxml/cxml.h>
39 #include <dcp/exceptions.h>
40 #include <dcp/warnings.h>
41 LIBDCP_DISABLE_WARNINGS
42 #include <wx/listctrl.h>
43 #include <wx/treectrl.h>
44 LIBDCP_ENABLE_WARNINGS
45
46
47 using std::exception;
48 using std::list;
49 using std::make_pair;
50 using std::map;
51 using std::pair;
52 using std::runtime_error;
53 using std::shared_ptr;
54 using std::string;
55 using std::vector;
56 using boost::bind;
57 using boost::optional;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
60 #endif
61
62
63 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
64         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
65         , _film (film)
66 {
67         /* Main sizers */
68         auto horizontal = new wxBoxSizer (wxHORIZONTAL);
69         auto left = new wxBoxSizer (wxVERTICAL);
70         auto right = new wxBoxSizer (wxVERTICAL);
71
72         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
73         horizontal->Add (right, 1, wxEXPAND);
74
75         /* Font for sub-headings */
76         wxFont subheading_font (*wxNORMAL_FONT);
77         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
78
79         /* Sub-heading: Screens */
80         auto h = new StaticText (this, _("Screens"));
81         h->SetFont (subheading_font);
82         left->Add (h, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
83         _screens = new ScreensPanel (this);
84         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
85
86         /* Sub-heading: Timing */
87         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
88         h = new StaticText (this, S_("KDM|Timing"));
89         h->SetFont (subheading_font);
90         right->Add (h);
91         _timing = new KDMTimingPanel (this);
92         right->Add (_timing);
93
94         /* Sub-heading: CPL */
95         h = new StaticText (this, _("CPL"));
96         h->SetFont (subheading_font);
97         right->Add (h);
98
99         vector<CPLSummary> cpls;
100         for (auto const& i: film->cpls()) {
101                 if (i.encrypted) {
102                         cpls.push_back (i);
103                 }
104         }
105
106         _cpl = new KDMCPLPanel (this, cpls);
107         right->Add (_cpl, 0, wxEXPAND);
108
109         /* Sub-heading: Output */
110         h = new StaticText (this, _("Output"));
111         h->SetFont (subheading_font);
112         right->Add(h, 0, wxTOP, DCPOMATIC_SUBHEADING_TOP_PAD);
113         _output = new KDMOutputPanel (this);
114         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
115
116         _make = new Button (this, _("Make KDMs"));
117         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
118
119         /* Make an overall sizer to get a nice border */
120
121         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
122         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
123
124         /* Bind */
125
126         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
127         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
128         _make->Bind (wxEVT_BUTTON, boost::bind (&KDMDialog::make_clicked, this));
129
130         setup_sensitivity ();
131
132         SetSizer (overall_sizer);
133         overall_sizer->Layout ();
134         overall_sizer->SetSizeHints (this);
135 }
136
137
138 void
139 KDMDialog::setup_sensitivity ()
140 {
141         _screens->setup_sensitivity ();
142         _output->setup_sensitivity ();
143         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
144 }
145
146
147 bool
148 KDMDialog::confirm_overwrite (boost::filesystem::path path)
149 {
150         return confirm_dialog (
151                 this,
152                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
153                 );
154 }
155
156
157 void
158 KDMDialog::make_clicked ()
159 {
160         auto film = _film.lock ();
161         DCPOMATIC_ASSERT (film);
162
163         list<KDMWithMetadataPtr> kdms;
164         try {
165                 /* Start off by enabling forensic marking for all */
166                 optional<int> for_audio;
167                 if (!_output->forensic_mark_audio()) {
168                         /* No forensic marking for audio */
169                         for_audio = 0;
170                 } else if (_output->forensic_mark_audio_up_to()) {
171                         /* Forensic mark up to this channel; disabled on channels greater than this */
172                         for_audio = _output->forensic_mark_audio_up_to();
173                 }
174
175                 vector<KDMCertificatePeriod> period_checks;
176
177                 std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm = [film, this](dcp::LocalTime begin, dcp::LocalTime end) {
178                         return film->make_kdm(_cpl->cpl(), begin, end);
179                 };
180
181                 for (auto i: _screens->screens()) {
182                         auto p = kdm_for_screen(make_kdm, i, _timing->from(), _timing->until(), _output->formulation(), !_output->forensic_mark_video(), for_audio, period_checks);
183                         if (p) {
184                                 kdms.push_back (p);
185                         }
186                 }
187
188                 if (find(period_checks.begin(), period_checks.end(), KDMCertificatePeriod::KDM_OUTSIDE_CERTIFICATE) != period_checks.end()) {
189                         error_dialog(
190                                 this,
191                                 _("Some KDMs would have validity periods which are completely outside the recipient certificate periods.  Such KDMs are very unlikely to work, so will not be created.")
192                                 );
193                         return;
194                 }
195
196                 if (find(period_checks.begin(), period_checks.end(), KDMCertificatePeriod::KDM_OVERLAPS_CERTIFICATE) != period_checks.end()) {
197                         message_dialog(
198                                 this,
199                                 _("For some of these KDMs the recipient certificate's validity period will not cover the whole of the KDM validity period.  This might cause problems with the KDMs.")
200                                 );
201                 }
202
203         } catch (dcp::BadKDMDateError& e) {
204                 if (e.starts_too_early()) {
205                         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."));
206                 } else {
207                         error_dialog (this, _("The KDM end period is after (or close to) the end of the signing certficates' validity period.  Either use an earlier end time for this KDM or re-create your signing certificates in the DCP-o-matic preferences window."));
208                 }
209                 return;
210         } catch (runtime_error& e) {
211                 error_dialog (this, std_to_wx(e.what()));
212                 return;
213         }
214
215         auto result = _output->make (kdms, film->name(), bind (&KDMDialog::confirm_overwrite, this, _1));
216         if (result.first) {
217                 JobManager::instance()->add (result.first);
218         }
219
220         if (result.second > 0) {
221                 /* XXX: proper plural form support in wxWidgets? */
222                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
223                 message_dialog (
224                         this,
225                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
226                         );
227         }
228 }