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