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