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