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