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