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