Write 'f', 'b' and 'e' tags into all KDMWithMetadata when they are made.
[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 #include "kdm_dialog.h"
22 #include "wx_util.h"
23 #include "screens_panel.h"
24 #include "kdm_timing_panel.h"
25 #include "kdm_output_panel.h"
26 #include "kdm_cpl_panel.h"
27 #include "confirm_kdm_email_dialog.h"
28 #include "static_text.h"
29 #include "dcpomatic_button.h"
30 #include "lib/film.h"
31 #include "lib/screen.h"
32 #include "lib/kdm_with_metadata.h"
33 #include "lib/job_manager.h"
34 #include "lib/config.h"
35 #include "lib/cinema.h"
36 #include <libcxml/cxml.h>
37 #include <dcp/exceptions.h>
38 #include <wx/treectrl.h>
39 #include <wx/listctrl.h>
40 #include <iostream>
41
42 using std::string;
43 using std::exception;
44 using std::map;
45 using std::list;
46 using std::pair;
47 using std::cout;
48 using std::vector;
49 using std::make_pair;
50 using std::runtime_error;
51 using boost::shared_ptr;
52 using boost::bind;
53 using boost::optional;
54
55 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
56         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
57         , _film (film)
58 {
59         /* Main sizers */
60         wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
61         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
62         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
63
64         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
65         horizontal->Add (right, 1, wxEXPAND);
66
67         /* Font for sub-headings */
68         wxFont subheading_font (*wxNORMAL_FONT);
69         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
70
71         /* Sub-heading: Screens */
72         wxStaticText* h = new StaticText (this, _("Screens"));
73         h->SetFont (subheading_font);
74         left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
75         _screens = new ScreensPanel (this);
76         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
77
78         /* Sub-heading: Timing */
79         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
80         h = new StaticText (this, S_("KDM|Timing"));
81         h->SetFont (subheading_font);
82         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
83         _timing = new KDMTimingPanel (this);
84         right->Add (_timing);
85
86         /* Sub-heading: CPL */
87         h = new StaticText (this, _("CPL"));
88         h->SetFont (subheading_font);
89         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
90
91         vector<CPLSummary> cpls;
92         BOOST_FOREACH (CPLSummary const & i, film->cpls()) {
93                 if (i.encrypted) {
94                         cpls.push_back (i);
95                 }
96         }
97
98         _cpl = new KDMCPLPanel (this, cpls);
99         right->Add (_cpl, 0, wxEXPAND);
100
101         /* Sub-heading: Output */
102         h = new StaticText (this, _("Output"));
103         h->SetFont (subheading_font);
104         right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
105         _output = new KDMOutputPanel (this, film->interop ());
106         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
107
108         _make = new Button (this, _("Make KDMs"));
109         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
110
111         /* Make an overall sizer to get a nice border */
112
113         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
114         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
115
116         /* Bind */
117
118         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
119         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
120         _make->Bind (wxEVT_BUTTON, boost::bind (&KDMDialog::make_clicked, this));
121
122         setup_sensitivity ();
123
124         SetSizer (overall_sizer);
125         overall_sizer->Layout ();
126         overall_sizer->SetSizeHints (this);
127 }
128
129 void
130 KDMDialog::setup_sensitivity ()
131 {
132         _screens->setup_sensitivity ();
133         _output->setup_sensitivity ();
134         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
135 }
136
137 bool
138 KDMDialog::confirm_overwrite (boost::filesystem::path path)
139 {
140         return confirm_dialog (
141                 this,
142                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
143                 );
144 }
145
146 void
147 KDMDialog::make_clicked ()
148 {
149         shared_ptr<const Film> film = _film.lock ();
150         DCPOMATIC_ASSERT (film);
151
152         list<KDMWithMetadataPtr> kdms;
153         try {
154                 /* Start off by enabling forensic marking for all */
155                 optional<int> for_audio;
156                 if (!_output->forensic_mark_audio()) {
157                         /* No forensic marking for audio */
158                         for_audio = 0;
159                 } else if (_output->forensic_mark_audio_up_to()) {
160                         /* Forensic mark up to this channel; disabled on channels greater than this */
161                         for_audio = _output->forensic_mark_audio_up_to();
162                 }
163
164                 BOOST_FOREACH (shared_ptr<dcpomatic::Screen> i, _screens->screens()) {
165                         if (i->recipient) {
166                                 dcp::LocalTime const begin(_timing->from(),  i->cinema ? i->cinema->utc_offset_hour() : 0, i->cinema ? i->cinema->utc_offset_minute() : 0);
167                                 dcp::LocalTime const end(_timing->until(), i->cinema ? i->cinema->utc_offset_hour() : 0, i->cinema ? i->cinema->utc_offset_minute() : 0);
168
169                                 dcp::EncryptedKDM const kdm = film->make_kdm (
170                                                 i->recipient.get(),
171                                                 i->trusted_device_thumbprints(),
172                                                 _cpl->cpl(),
173                                                 begin,
174                                                 end,
175                                                 _output->formulation(),
176                                                 !_output->forensic_mark_video(),
177                                                 for_audio
178                                                 );
179
180                                 dcp::NameFormat::Map name_values;
181                                 if (i->cinema) {
182                                         name_values['c'] = i->cinema->name;
183                                 }
184                                 name_values['s'] = i->name;
185                                 name_values['f'] = film->name();
186                                 name_values['b'] = dcp::LocalTime(begin).date() + " " + dcp::LocalTime(begin).time_of_day(false, false);
187                                 name_values['e'] = dcp::LocalTime(end).date() + " " + dcp::LocalTime(end).time_of_day(false, false);
188
189                                 kdms.push_back (KDMWithMetadataPtr(new DCPKDMWithMetadata(name_values, i->cinema, kdm)));
190                         }
191                 }
192
193         } catch (dcp::BadKDMDateError& e) {
194                 if (e.starts_too_early()) {
195                         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."));
196                 } else {
197                         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."));
198                 }
199                 return;
200         } catch (runtime_error& e) {
201                 error_dialog (this, std_to_wx(e.what()));
202                 return;
203         }
204
205         pair<shared_ptr<Job>, int> result = _output->make (kdms, film->name(), bind (&KDMDialog::confirm_overwrite, this, _1));
206         if (result.first) {
207                 JobManager::instance()->add (result.first);
208         }
209
210         if (result.second > 0) {
211                 /* XXX: proper plural form support in wxWidgets? */
212                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
213                 message_dialog (
214                         this,
215                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
216                         );
217         }
218 }