7b739720f326c67461174f711353d1276b4d55b0
[dcpomatic.git] / src / wx / kdm_output_panel.cc
1 /*
2     Copyright (C) 2015-2022 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 "check_box.h"
23 #include "confirm_kdm_email_dialog.h"
24 #include "dcpomatic_button.h"
25 #include "kdm_advanced_dialog.h"
26 #include "kdm_output_panel.h"
27 #include "kdm_timing_panel.h"
28 #include "name_format_editor.h"
29 #include "wx_util.h"
30 #include "lib/cinema.h"
31 #include "lib/config.h"
32 #include "lib/send_kdm_email_job.h"
33 #include "lib/warnings.h"
34 #include <dcp/exceptions.h>
35 #include <dcp/types.h>
36 #ifdef DCPOMATIC_USE_OWN_PICKER
37 #include "dir_picker_ctrl.h"
38 #else
39 DCPOMATIC_DISABLE_WARNINGS
40 #include <wx/filepicker.h>
41 DCPOMATIC_ENABLE_WARNINGS
42 #endif
43 DCPOMATIC_DISABLE_WARNINGS
44 #include <wx/stdpaths.h>
45 DCPOMATIC_ENABLE_WARNINGS
46
47
48 using std::exception;
49 using std::function;
50 using std::list;
51 using std::make_pair;
52 using std::make_shared;
53 using std::pair;
54 using std::shared_ptr;
55 using std::string;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
58 #endif
59
60
61 KDMOutputPanel::KDMOutputPanel (wxWindow* parent)
62         : wxPanel (parent, wxID_ANY)
63         , _forensic_mark_video (true)
64         , _forensic_mark_audio (true)
65         , _forensic_mark_audio_up_to (12)
66 {
67         auto table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, 0);
68         table->AddGrowableCol (1);
69
70         add_label_to_sizer (table, this, _("KDM type"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
71
72         auto type = new wxBoxSizer (wxHORIZONTAL);
73         _type = new wxChoice (this, wxID_ANY);
74         _type->Append ("Modified Transitional 1", ((void *) dcp::Formulation::MODIFIED_TRANSITIONAL_1));
75         _type->Append ("DCI Any", ((void *) dcp::Formulation::DCI_ANY));
76         _type->Append ("DCI Specific", ((void *) dcp::Formulation::DCI_SPECIFIC));
77         _type->Append ("Multiple Modified Transitional 1", ((void *) dcp::Formulation::MULTIPLE_MODIFIED_TRANSITIONAL_1));
78         _type->Append ("Modified Transitional 1 (without AuthorizedDeviceInfo)", ((void *) dcp::Formulation::MODIFIED_TRANSITIONAL_TEST));
79         type->Add (_type, 1, wxTOP, DCPOMATIC_CHOICE_TOP_PAD);
80         _type->SetSelection (0);
81         auto advanced = new Button (this, _("Advanced..."));
82         type->Add (advanced, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
83         table->Add (type, 1, wxTOP, DCPOMATIC_CHOICE_TOP_PAD);
84
85         add_label_to_sizer (table, this, _("Folder / ZIP name format"), true, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT);
86         _container_name_format = new NameFormatEditor (this, Config::instance()->kdm_container_name_format(), dcp::NameFormat::Map(), dcp::NameFormat::Map(), "");
87         table->Add (_container_name_format->panel(), 1, wxEXPAND);
88
89         auto format = create_label (this, _("Filename format"), true);
90         auto align = new wxBoxSizer (wxHORIZONTAL);
91 #ifdef DCPOMATIC_OSX
92         align->Add (format, 0, wxTOP, 2);
93         table->Add (align, 0, wxALIGN_RIGHT | wxRIGHT, DCPOMATIC_SIZER_GAP - 2);
94 #else
95         align->Add (format, 0, wxLEFT, DCPOMATIC_SIZER_GAP - 2);
96         table->Add (align, 0, wxTOP | wxRIGHT | wxALIGN_TOP, DCPOMATIC_SIZER_GAP);
97 #endif
98         dcp::NameFormat::Map titles;
99         titles['f'] = wx_to_std (_("film name"));
100         titles['c'] = wx_to_std (_("cinema"));
101         titles['s'] = wx_to_std (_("screen"));
102         titles['b'] = wx_to_std (_("from date/time"));
103         titles['e'] = wx_to_std (_("to date/time"));
104         dcp::NameFormat::Map ex;
105         ex['f'] = "Bambi";
106         ex['c'] = "Lumière";
107         ex['s'] = "Screen 1";
108         ex['b'] = "2012/03/15 12:30";
109         ex['e'] = "2012/03/22 02:30";
110         _filename_format = new NameFormatEditor (this, Config::instance()->kdm_filename_format(), titles, ex, ".xml");
111         table->Add (_filename_format->panel(), 1, wxEXPAND);
112
113         _write_to = new CheckBox (this, _("Write to"));
114         table->Add (_write_to, 1, wxEXPAND);
115
116 #ifdef DCPOMATIC_USE_OWN_PICKER
117         _folder = new DirPickerCtrl (this);
118 #else
119         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
120 #endif
121
122         auto path = Config::instance()->default_kdm_directory();
123         if (path) {
124                 _folder->SetPath (std_to_wx (path->string ()));
125         } else {
126                 _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
127         }
128
129         table->Add (_folder, 1, wxEXPAND);
130
131         auto write_options = new wxBoxSizer(wxVERTICAL);
132         _write_flat = new wxRadioButton (this, wxID_ANY, _("Write all KDMs to the same folder"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
133         write_options->Add (_write_flat, 1, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
134         _write_folder = new wxRadioButton (this, wxID_ANY, _("Write a folder for each cinema's KDMs"));
135         write_options->Add (_write_folder, 1, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
136         _write_zip = new wxRadioButton (this, wxID_ANY, _("Write a ZIP file for each cinema's KDMs"));
137         write_options->Add (_write_zip, 1, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
138         table->AddSpacer (0);
139         table->Add (write_options);
140
141         _email = new CheckBox (this, _("Send by email"));
142         table->Add (_email, 1, wxEXPAND);
143         table->AddSpacer (0);
144
145         switch (Config::instance()->last_kdm_write_type().get_value_or(Config::KDM_WRITE_FLAT)) {
146         case Config::KDM_WRITE_FLAT:
147                 _write_flat->SetValue (true);
148                 break;
149         case Config::KDM_WRITE_FOLDER:
150                 _write_folder->SetValue (true);
151                 break;
152         case Config::KDM_WRITE_ZIP:
153                 _write_zip->SetValue (true);
154                 break;
155         }
156
157         _write_to->SetValue (Config::instance()->write_kdms_to_disk());
158         _email->SetValue (Config::instance()->email_kdms());
159
160         _write_to->Bind     (wxEVT_CHECKBOX, boost::bind (&KDMOutputPanel::write_to_changed, this));
161         _email->Bind        (wxEVT_CHECKBOX, boost::bind (&KDMOutputPanel::email_changed, this));
162         _write_flat->Bind   (wxEVT_RADIOBUTTON, boost::bind (&KDMOutputPanel::kdm_write_type_changed, this));
163         _write_folder->Bind (wxEVT_RADIOBUTTON, boost::bind (&KDMOutputPanel::kdm_write_type_changed, this));
164         _write_zip->Bind    (wxEVT_RADIOBUTTON, boost::bind (&KDMOutputPanel::kdm_write_type_changed, this));
165         advanced->Bind      (wxEVT_BUTTON, boost::bind (&KDMOutputPanel::advanced_clicked, this));
166
167         SetSizer (table);
168 }
169
170
171 void
172 KDMOutputPanel::write_to_changed ()
173 {
174         Config::instance()->set_write_kdms_to_disk(_write_to->GetValue());
175         setup_sensitivity ();
176 }
177
178
179 void
180 KDMOutputPanel::email_changed ()
181 {
182         Config::instance()->set_email_kdms(_email->GetValue());
183         setup_sensitivity ();
184 }
185
186
187 void
188 KDMOutputPanel::setup_sensitivity ()
189 {
190         bool const write = _write_to->GetValue ();
191         _folder->Enable (write);
192         _write_flat->Enable (write);
193         _write_folder->Enable (write);
194         _write_zip->Enable (write);
195 }
196
197
198 void
199 KDMOutputPanel::advanced_clicked ()
200 {
201         auto d = new KDMAdvancedDialog (this, _forensic_mark_video, _forensic_mark_audio, _forensic_mark_audio_up_to);
202         d->ShowModal ();
203         _forensic_mark_video = d->forensic_mark_video ();
204         _forensic_mark_audio = d->forensic_mark_audio ();
205         _forensic_mark_audio_up_to = d->forensic_mark_audio_up_to ();
206         d->Destroy ();
207 }
208
209
210 void
211 KDMOutputPanel::kdm_write_type_changed ()
212 {
213         if (_write_flat->GetValue()) {
214                 Config::instance()->set_last_kdm_write_type (Config::KDM_WRITE_FLAT);
215         } else if (_write_folder->GetValue()) {
216                 Config::instance()->set_last_kdm_write_type (Config::KDM_WRITE_FOLDER);
217         } else if (_write_zip->GetValue()) {
218                 Config::instance()->set_last_kdm_write_type (Config::KDM_WRITE_ZIP);
219         }
220 }
221
222
223 pair<shared_ptr<Job>, int>
224 KDMOutputPanel::make (
225         list<KDMWithMetadataPtr> kdms, string name, function<bool (boost::filesystem::path)> confirm_overwrite
226         )
227 {
228         auto const cinema_kdms = collect (kdms);
229
230         /* Decide whether to proceed */
231
232         bool proceed = true;
233
234         if (_email->GetValue ()) {
235
236                 if (Config::instance()->mail_server().empty ()) {
237                         proceed = false;
238                         error_dialog (this, _("You must set up a mail server in Preferences before you can send emails."));
239                 }
240
241                 bool const cinemas_with_no_email = std::any_of(
242                         cinema_kdms.begin(), cinema_kdms.end(),
243                         [](list<KDMWithMetadataPtr> const& list) { return list.front()->emails().empty(); }
244                         );
245
246                 if (proceed && cinemas_with_no_email && !confirm_dialog (
247                             this,
248                             _("You have selected some cinemas that have no configured email address.  Do you want to continue?")
249                             )) {
250                         proceed = false;
251                 }
252
253                 if (proceed && Config::instance()->confirm_kdm_email ()) {
254                         list<string> emails;
255                         for (auto const& i: cinema_kdms) {
256                                 for (auto j: i.front()->emails()) {
257                                         emails.push_back (j);
258                                 }
259                         }
260
261                         if (!emails.empty ()) {
262                                 auto d = new ConfirmKDMEmailDialog (this, emails);
263                                 if (d->ShowModal() == wxID_CANCEL) {
264                                         proceed = false;
265                                 }
266                                 d->Destroy ();
267                         }
268                 }
269         }
270
271         if (!proceed) {
272                 return {};
273         }
274
275         Config::instance()->set_kdm_filename_format (_filename_format->get ());
276
277         int written = 0;
278         shared_ptr<Job> job;
279
280         try {
281
282                 if (_write_to->GetValue()) {
283                         if (_write_flat->GetValue()) {
284                                 written = write_files (
285                                         kdms,
286                                         directory(),
287                                         _filename_format->get(),
288                                         confirm_overwrite
289                                         );
290                         } else if (_write_folder->GetValue()) {
291                                 written = write_directories (
292                                         collect (kdms),
293                                         directory(),
294                                         _container_name_format->get(),
295                                         _filename_format->get(),
296                                         confirm_overwrite
297                                         );
298                         } else if (_write_zip->GetValue()) {
299                                 written = write_zip_files (
300                                         collect (kdms),
301                                         directory(),
302                                         _container_name_format->get(),
303                                         _filename_format->get(),
304                                         confirm_overwrite
305                                         );
306                         }
307                 }
308
309                 if (_email->GetValue ()) {
310                         job = make_shared<SendKDMEmailJob>(
311                                 cinema_kdms,
312                                 _container_name_format->get(),
313                                 _filename_format->get(),
314                                 name
315                                 );
316                 }
317
318         } catch (dcp::NotEncryptedError& e) {
319                 error_dialog (this, _("CPL's content is not encrypted."));
320         } catch (exception& e) {
321                 error_dialog (this, std_to_wx(e.what()));
322         } catch (...) {
323                 error_dialog (this, _("An unknown exception occurred."));
324         }
325
326         return make_pair (job, written);
327 }
328
329
330 dcp::Formulation
331 KDMOutputPanel::formulation () const
332 {
333         return (dcp::Formulation) reinterpret_cast<intptr_t> (_type->GetClientData (_type->GetSelection()));
334 }
335
336
337 boost::filesystem::path
338 KDMOutputPanel::directory () const
339 {
340         return wx_to_std (_folder->GetPath ());
341 }