Supporters update.
[dcpomatic.git] / src / wx / dkdm_dialog.cc
1 /*
2     Copyright (C) 2012-2021 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 "dkdm_dialog.h"
25 #include "dkdm_output_panel.h"
26 #include "kdm_cpl_panel.h"
27 #include "kdm_timing_panel.h"
28 #include "recipients_panel.h"
29 #include "static_text.h"
30 #include "wx_util.h"
31 #include "lib/config.h"
32 #include "lib/film.h"
33 #include "lib/job_manager.h"
34 #include "lib/kdm_with_metadata.h"
35 #include <libcxml/cxml.h>
36 #include <dcp/exceptions.h>
37 #include <dcp/warnings.h>
38 LIBDCP_DISABLE_WARNINGS
39 #include <wx/listctrl.h>
40 #include <wx/treectrl.h>
41 LIBDCP_ENABLE_WARNINGS
42
43
44 using std::exception;
45 using std::list;
46 using std::make_pair;
47 using std::map;
48 using std::pair;
49 using std::runtime_error;
50 using std::shared_ptr;
51 using std::string;
52 using std::vector;
53 using boost::bind;
54 using boost::optional;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
57 #endif
58
59
60 DKDMDialog::DKDMDialog (wxWindow* parent, shared_ptr<const Film> film)
61         : wxDialog (parent, wxID_ANY, _("Make DKDMs"))
62         , _film (film)
63 {
64         /* Main sizers */
65         auto horizontal = new wxBoxSizer (wxHORIZONTAL);
66         auto left = new wxBoxSizer (wxVERTICAL);
67         auto right = new wxBoxSizer (wxVERTICAL);
68
69         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
70         horizontal->Add (right, 1, wxEXPAND);
71
72         /* Font for sub-headings */
73         wxFont subheading_font (*wxNORMAL_FONT);
74         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
75
76         /* Sub-heading: Screens */
77         auto h = new StaticText (this, _("Recipients"));
78         h->SetFont (subheading_font);
79         left->Add (h, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
80         _recipients = new RecipientsPanel (this);
81         left->Add (_recipients, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
82
83         /* Sub-heading: Timing */
84         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
85         h = new StaticText (this, S_("KDM|Timing"));
86         h->SetFont (subheading_font);
87         right->Add (h);
88         _timing = new KDMTimingPanel (this);
89         right->Add (_timing);
90
91         /* Sub-heading: CPL */
92         h = new StaticText (this, _("CPL"));
93         h->SetFont (subheading_font);
94         right->Add (h);
95
96         vector<CPLSummary> cpls;
97         for (auto const& i: film->cpls()) {
98                 if (i.encrypted) {
99                         cpls.push_back (i);
100                 }
101         }
102
103         _cpl = new KDMCPLPanel (this, cpls);
104         right->Add (_cpl, 0, wxEXPAND);
105
106         /* Sub-heading: Output */
107         h = new StaticText (this, _("Output"));
108         h->SetFont (subheading_font);
109         right->Add(h, 0, wxTOP, DCPOMATIC_SUBHEADING_TOP_PAD);
110         _output = new DKDMOutputPanel (this);
111         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
112
113         _make = new Button (this, _("Make DKDMs"));
114         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
115
116         /* Make an overall sizer to get a nice border */
117
118         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
119         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
120
121         /* Bind */
122
123         _recipients->RecipientsChanged.connect (boost::bind(&DKDMDialog::setup_sensitivity, this));
124         _timing->TimingChanged.connect (boost::bind(&DKDMDialog::setup_sensitivity, this));
125         _make->Bind (wxEVT_BUTTON, boost::bind(&DKDMDialog::make_clicked, this));
126         _cpl->Changed.connect(boost::bind(&DKDMDialog::setup_sensitivity, 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 DKDMDialog::setup_sensitivity ()
138 {
139         _recipients->setup_sensitivity ();
140         _output->setup_sensitivity ();
141         _make->Enable (!_recipients->recipients().empty() && _timing->valid() && _cpl->has_selected());
142 }
143
144
145 bool
146 DKDMDialog::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 DKDMDialog::make_clicked ()
157 {
158         auto film = _film.lock ();
159         DCPOMATIC_ASSERT (film);
160
161         list<KDMWithMetadataPtr> kdms;
162         try {
163                 for (auto i: _recipients->recipients()) {
164                         auto p = kdm_for_dkdm_recipient (film, _cpl->cpl(), i, _timing->from(), _timing->until());
165                         if (p) {
166                                 kdms.push_back (p);
167                         }
168                 }
169         } catch (dcp::BadKDMDateError& e) {
170                 if (e.starts_too_early()) {
171                         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."));
172                 } else {
173                         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."));
174                 }
175                 return;
176         } catch (runtime_error& e) {
177                 error_dialog (this, std_to_wx(e.what()));
178                 return;
179         }
180
181         auto result = _output->make(kdms, film->dcp_name(), bind(&DKDMDialog::confirm_overwrite, this, _1));
182         if (result.first) {
183                 JobManager::instance()->add (result.first);
184         }
185
186         if (result.second > 0) {
187                 /* XXX: proper plural form support in wxWidgets? */
188                 auto s = result.second == 1 ? _("%d DKDM written to %s") : _("%d DKDMs written to %s");
189                 message_dialog (
190                         this,
191                         wxString::Format(s, result.second, std_to_wx(_output->directory().string()).data())
192                         );
193         }
194 }