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