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