2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "wx/config_dialog.h"
22 #include "wx/about_dialog.h"
23 #include "wx/report_problem_dialog.h"
24 #include "wx/file_picker_ctrl.h"
25 #include "wx/wx_util.h"
26 #include "wx/wx_signal_manager.h"
27 #include "wx/screens_panel.h"
28 #include "wx/kdm_timing_panel.h"
29 #include "wx/kdm_output_panel.h"
30 #include "wx/job_view_dialog.h"
31 #include "wx/file_dialog_wrapper.h"
32 #include "wx/editable_list.h"
33 #include "lib/config.h"
35 #include "lib/screen.h"
36 #include "lib/job_manager.h"
37 #include "lib/screen_kdm.h"
38 #include "lib/exceptions.h"
39 #include "lib/cinema_kdms.h"
40 #include "lib/send_kdm_email_job.h"
41 #include "lib/compose.hpp"
42 #include "lib/cinema.h"
43 #include <dcp/encrypted_kdm.h>
44 #include <dcp/decrypted_kdm.h>
45 #include <dcp/exceptions.h>
47 #include <wx/preferences.h>
48 #include <wx/filepicker.h>
50 #include <ApplicationServices/ApplicationServices.h>
52 #include <boost/bind.hpp>
53 #include <boost/foreach.hpp>
63 using boost::shared_ptr;
67 ID_help_report_a_problem = 1,
70 class KDMFileDialogWrapper : public FileDialogWrapper<dcp::EncryptedKDM>
73 KDMFileDialogWrapper (wxWindow* parent)
74 : FileDialogWrapper<dcp::EncryptedKDM> (parent, _("Select DKDM file"))
81 column (dcp::EncryptedKDM k)
83 return String::compose ("%1 (%2)", k.content_title_text(), k.cpl_id());
86 class DOMFrame : public wxFrame
89 DOMFrame (wxString const & title)
90 : wxFrame (0, -1, title)
94 #if defined(DCPOMATIC_WINDOWS)
95 if (Config::instance()->win32_console ()) {
98 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
99 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
100 FILE* hf_out = _fdopen(hCrt, "w");
101 setvbuf(hf_out, NULL, _IONBF, 1);
104 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
105 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
106 FILE* hf_in = _fdopen(hCrt, "r");
107 setvbuf(hf_in, NULL, _IONBF, 128);
110 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
114 wxMenuBar* bar = new wxMenuBar;
118 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
119 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
120 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
121 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
123 /* Use a panel as the only child of the Frame so that we avoid
124 the dark-grey background on Windows.
126 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
127 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
129 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
131 wxFont subheading_font (*wxNORMAL_FONT);
132 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
134 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
135 h->SetFont (subheading_font);
136 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
137 _screens = new ScreensPanel (overall_panel);
138 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
140 _timing = new KDMTimingPanel (overall_panel);
141 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
143 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
144 h->SetFont (subheading_font);
145 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
147 vector<string> columns;
148 columns.push_back (wx_to_std (_("CPL")));
149 _dkdm = new EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper> (
150 overall_panel, columns, bind (&DOMFrame::dkdms, this), bind (&DOMFrame::set_dkdms, this, _1), bind (&always_valid), bind (&column, _1), false
152 vertical->Add (_dkdm, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
154 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
155 h->SetFont (subheading_font);
156 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
157 /* XXX: hard-coded non-interop here */
158 _output = new KDMOutputPanel (overall_panel, false);
159 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
161 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
162 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
164 main_sizer->Add (vertical, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
165 overall_panel->SetSizer (main_sizer);
167 /* Instantly save any config changes when using a DCP-o-matic GUI */
168 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
170 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
171 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
172 _dkdm->SelectionChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
174 setup_sensitivity ();
178 vector<dcp::EncryptedKDM> dkdms () const
180 return Config::instance()->dkdms ();
183 void set_dkdms (vector<dcp::EncryptedKDM> dkdms)
185 Config::instance()->set_dkdms (dkdms);
190 /* false here allows the close handler to veto the close request */
194 void edit_preferences ()
196 if (!_config_dialog) {
197 _config_dialog = create_config_dialog ();
199 _config_dialog->Show (this);
204 AboutDialog* d = new AboutDialog (this);
209 void help_report_a_problem ()
211 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
212 if (d->ShowModal () == wxID_OK) {
218 void setup_menu (wxMenuBar* m)
220 wxMenu* file = new wxMenu;
223 file->Append (wxID_EXIT, _("&Exit"));
225 file->Append (wxID_EXIT, _("&Quit"));
229 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
231 wxMenu* edit = new wxMenu;
232 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
235 wxMenu* help = new wxMenu;
237 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
239 help->Append (wxID_ABOUT, _("About"));
241 help->Append (ID_help_report_a_problem, _("Report a problem..."));
243 m->Append (file, _("&File"));
245 m->Append (edit, _("&Edit"));
247 m->Append (help, _("&Help"));
253 if (!_dkdm->selection()) {
257 /* Decrypt the DKDM */
258 dcp::DecryptedKDM decrypted (_dkdm->selection().get(), Config::instance()->decryption_chain()->key().get());
260 /* This is the signer for our new KDMs */
261 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
262 if (!signer->valid ()) {
263 throw InvalidSignerError ();
266 list<ScreenKDM> screen_kdms;
267 BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
273 /* Make an empty KDM */
274 dcp::DecryptedKDM kdm (
275 dcp::LocalTime (_timing->from(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
276 dcp::LocalTime (_timing->until(), i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
277 decrypted.annotation_text().get_value_or (""),
278 decrypted.content_title_text(),
279 dcp::LocalTime().as_string()
282 /* Add keys from the DKDM */
283 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
288 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->recipient.get(), i->trusted_devices, _output->formulation())));
291 if (_output->write_to()) {
292 ScreenKDM::write_files (decrypted.content_title_text(), screen_kdms, _output->directory());
293 /* XXX: proper plural form support in wxWidgets? */
294 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
297 wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
300 string film_name = decrypted.annotation_text().get_value_or ("");
301 if (film_name.empty ()) {
302 film_name = decrypted.content_title_text ();
304 shared_ptr<Job> job (new SendKDMEmailJob (
306 decrypted.content_title_text(),
307 _timing->from(), _timing->until(),
308 CinemaKDMs::collect (screen_kdms),
312 JobManager::instance()->add (job);
314 _job_view->Destroy ();
317 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
318 _job_view->ShowModal ();
320 } catch (dcp::NotEncryptedError& e) {
321 error_dialog (this, _("CPL's content is not encrypted."));
322 } catch (exception& e) {
323 error_dialog (this, e.what ());
325 error_dialog (this, _("An unknown exception occurred."));
329 void setup_sensitivity ()
331 _screens->setup_sensitivity ();
332 _output->setup_sensitivity ();
333 _create->Enable (!_screens->screens().empty() && _dkdm->selection());
336 wxPreferencesEditor* _config_dialog;
337 ScreensPanel* _screens;
338 KDMTimingPanel* _timing;
339 EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper>* _dkdm;
341 KDMOutputPanel* _output;
342 JobViewDialog* _job_view;
346 * @brief The magic App class for wxWidgets.
348 class App : public wxApp
361 wxInitAllImageHandlers ();
363 SetAppName (_("DCP-o-matic KDM Creator"));
365 if (!wxApp::OnInit()) {
369 #ifdef DCPOMATIC_LINUX
370 unsetenv ("UBUNTU_MENUPROXY");
374 ProcessSerialNumber serial;
375 GetCurrentProcess (&serial);
376 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
379 dcpomatic_setup_path_encoding ();
381 /* Enable i18n; this will create a Config object
382 to look for a force-configured language. This Config
383 object will be wrong, however, because dcpomatic_setup
384 hasn't yet been called and there aren't any filters etc.
387 dcpomatic_setup_i18n ();
389 /* Set things up, including filters etc.
390 which will now be internationalised correctly.
394 /* Force the configuration to be re-loaded correctly next
399 _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
400 SetTopWindow (_frame);
404 signal_manager = new wxSignalManager (this);
405 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
411 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
415 /* An unhandled exception has occurred inside the main event loop */
416 bool OnExceptionInMainLoop ()
420 } catch (FileError& e) {
424 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
425 std_to_wx (e.what()),
426 std_to_wx (e.file().string().c_str ())
429 } catch (exception& e) {
433 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
434 std_to_wx (e.what ())
438 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
441 /* This will terminate the program */
445 void OnUnhandledException ()
447 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
452 signal_manager->ui_idle ();