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