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