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