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