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