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