2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "wx/config_dialog.h"
21 #include "wx/about_dialog.h"
22 #include "wx/report_problem_dialog.h"
23 #include "wx/file_picker_ctrl.h"
24 #include "wx/wx_util.h"
25 #include "wx/wx_signal_manager.h"
26 #include "wx/screens_panel.h"
27 #include "wx/kdm_timing_panel.h"
28 #include "wx/kdm_output_panel.h"
29 #include "wx/job_view_dialog.h"
30 #include "lib/config.h"
32 #include "lib/screen.h"
33 #include "lib/job_manager.h"
34 #include "lib/screen_kdm.h"
35 #include "lib/exceptions.h"
36 #include "lib/cinema_kdms.h"
37 #include "lib/send_kdm_email_job.h"
38 #include <dcp/encrypted_kdm.h>
39 #include <dcp/decrypted_kdm.h>
40 #include <dcp/exceptions.h>
42 #include <wx/preferences.h>
43 #include <wx/filepicker.h>
45 #include <ApplicationServices/ApplicationServices.h>
47 #include <boost/bind.hpp>
48 #include <boost/foreach.hpp>
57 using boost::shared_ptr;
61 ID_help_report_a_problem = 1,
64 class DOMFrame : public wxFrame
67 DOMFrame (wxString const & title)
68 : wxFrame (NULL, -1, title)
72 #if defined(DCPOMATIC_WINDOWS)
73 if (Config::instance()->win32_console ()) {
76 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
77 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
78 FILE* hf_out = _fdopen(hCrt, "w");
79 setvbuf(hf_out, NULL, _IONBF, 1);
82 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
83 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
84 FILE* hf_in = _fdopen(hCrt, "r");
85 setvbuf(hf_in, NULL, _IONBF, 128);
88 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
92 wxMenuBar* bar = new wxMenuBar;
96 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
97 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
98 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
99 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
101 /* Use a panel as the only child of the Frame so that we avoid
102 the dark-grey background on Windows.
104 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
105 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
107 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
109 wxFont subheading_font (*wxNORMAL_FONT);
110 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
112 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
113 h->SetFont (subheading_font);
114 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
115 _screens = new ScreensPanel (overall_panel);
116 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
118 _timing = new KDMTimingPanel (overall_panel);
119 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
121 wxSizer* dkdm = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
122 add_label_to_sizer (dkdm, overall_panel, _("DKDM file"), true);
123 _dkdm = new FilePickerCtrl (overall_panel, _("Select a DKDM XML file..."), "*.xml");
124 dkdm->Add (_dkdm, 1, wxEXPAND);
125 add_label_to_sizer (dkdm, overall_panel, _("Content title"), true);
126 _content_title_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
127 dkdm->Add (_content_title_text, 1, wxEXPAND);
128 add_label_to_sizer (dkdm, overall_panel, _("Annotation"), true);
129 _annotation_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
130 dkdm->Add (_annotation_text, 1, wxEXPAND);
131 add_label_to_sizer (dkdm, overall_panel, _("Issue date"), true);
132 _issue_date = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
133 dkdm->Add (_issue_date, 1, wxEXPAND);
134 vertical->Add (dkdm, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
136 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
137 h->SetFont (subheading_font);
138 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
139 /* XXX: hard-coded non-interop here */
140 _output = new KDMOutputPanel (overall_panel, false);
141 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
143 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
144 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
146 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
147 overall_panel->SetSizer (main_sizer);
149 /* Instantly save any config changes when using a DCP-o-matic GUI */
150 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
152 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
153 _dkdm->Bind (wxEVT_COMMAND_FILEPICKER_CHANGED, bind (&DOMFrame::dkdm_changed, this));
154 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
156 setup_sensitivity ();
162 /* false here allows the close handler to veto the close request */
166 void edit_preferences ()
168 if (!_config_dialog) {
169 _config_dialog = create_config_dialog ();
171 _config_dialog->Show (this);
176 AboutDialog* d = new AboutDialog (this);
181 void help_report_a_problem ()
183 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
184 if (d->ShowModal () == wxID_OK) {
190 void setup_menu (wxMenuBar* m)
192 wxMenu* file = new wxMenu;
195 file->Append (wxID_EXIT, _("&Exit"));
197 file->Append (wxID_EXIT, _("&Quit"));
201 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
203 wxMenu* edit = new wxMenu;
204 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
207 wxMenu* help = new wxMenu;
209 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
211 help->Append (wxID_ABOUT, _("About"));
213 help->Append (ID_help_report_a_problem, _("Report a problem..."));
215 m->Append (file, _("&File"));
217 m->Append (edit, _("&Edit"));
219 m->Append (help, _("&Help"));
224 if (_dkdm->GetPath().IsEmpty()) {
229 dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
230 dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
231 _annotation_text->Enable (true);
232 _annotation_text->SetLabel (std_to_wx (decrypted.annotation_text ()));
233 _content_title_text->Enable (true);
234 _content_title_text->SetLabel (std_to_wx (decrypted.content_title_text ()));
235 _issue_date->Enable (true);
236 _issue_date->SetLabel (std_to_wx (decrypted.issue_date ()));
237 } catch (exception& e) {
238 error_dialog (this, wxString::Format (_("Could not load DKDM (%s)"), std_to_wx (e.what()).data()));
239 _dkdm->SetPath (wxT(""));
240 _annotation_text->SetLabel (wxT(""));
241 _annotation_text->Enable (false);
242 _content_title_text->SetLabel (wxT(""));
243 _content_title_text->Enable (false);
244 _issue_date->SetLabel (wxT(""));
245 _issue_date->Enable (false);
248 setup_sensitivity ();
254 /* Decrypt the DKDM */
255 dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
256 dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
258 /* This is the signer for our new KDMs */
259 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
260 if (!signer->valid ()) {
261 throw InvalidSignerError ();
264 list<ScreenKDM> screen_kdms;
265 BOOST_FOREACH (shared_ptr<Screen> i, _screens->screens()) {
271 /* Make an empty KDM */
272 dcp::DecryptedKDM kdm (
273 _timing->from(), _timing->until(), decrypted.annotation_text(), decrypted.content_title_text(), dcp::LocalTime().as_string()
276 /* Add keys from the DKDM */
277 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, decrypted.keys()) {
282 screen_kdms.push_back (ScreenKDM (i, kdm.encrypt (signer, i->recipient.get(), i->trusted_devices, _output->formulation())));
285 if (_output->write_to()) {
286 ScreenKDM::write_files (decrypted.content_title_text(), screen_kdms, _output->directory());
287 /* XXX: proper plural form support in wxWidgets? */
288 wxString s = screen_kdms.size() == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
291 wxString::Format (s, int(screen_kdms.size()), std_to_wx(_output->directory().string()).data())
294 string film_name = decrypted.annotation_text ();
295 if (film_name.empty ()) {
296 film_name = decrypted.content_title_text ();
298 shared_ptr<Job> job (new SendKDMEmailJob (
300 decrypted.content_title_text(),
301 _timing->from(), _timing->until(),
302 CinemaKDMs::collect (screen_kdms),
306 JobManager::instance()->add (job);
308 _job_view->Destroy ();
311 _job_view = new JobViewDialog (this, _("Send KDM emails"), job);
312 _job_view->ShowModal ();
314 } catch (dcp::NotEncryptedError& e) {
315 error_dialog (this, _("CPL's content is not encrypted."));
316 } catch (exception& e) {
317 error_dialog (this, e.what ());
319 error_dialog (this, _("An unknown exception occurred."));
323 void setup_sensitivity ()
325 _screens->setup_sensitivity ();
326 _output->setup_sensitivity ();
327 _create->Enable (!_screens->screens().empty() && !_dkdm->GetPath().IsEmpty());
330 wxPreferencesEditor* _config_dialog;
331 ScreensPanel* _screens;
332 KDMTimingPanel* _timing;
333 /* I can't seem to clear the value in a wxFilePickerCtrl, so use our own */
334 FilePickerCtrl* _dkdm;
335 wxStaticText* _annotation_text;
336 wxStaticText* _content_title_text;
337 wxStaticText* _issue_date;
339 KDMOutputPanel* _output;
340 JobViewDialog* _job_view;
344 * @brief The magic App class for wxWidgets.
346 class App : public wxApp
359 wxInitAllImageHandlers ();
361 SetAppName (_("DCP-o-matic KDM creator"));
363 if (!wxApp::OnInit()) {
367 #ifdef DCPOMATIC_LINUX
368 unsetenv ("UBUNTU_MENUPROXY");
372 ProcessSerialNumber serial;
373 GetCurrentProcess (&serial);
374 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
377 dcpomatic_setup_path_encoding ();
379 /* Enable i18n; this will create a Config object
380 to look for a force-configured language. This Config
381 object will be wrong, however, because dcpomatic_setup
382 hasn't yet been called and there aren't any filters etc.
385 dcpomatic_setup_i18n ();
387 /* Set things up, including filters etc.
388 which will now be internationalised correctly.
392 /* Force the configuration to be re-loaded correctly next
397 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
398 SetTopWindow (_frame);
402 signal_manager = new wxSignalManager (this);
403 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
409 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
413 /* An unhandled exception has occurred inside the main event loop */
414 bool OnExceptionInMainLoop ()
418 } catch (FileError& e) {
422 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
423 std_to_wx (e.what()),
424 std_to_wx (e.file().string().c_str ())
427 } catch (exception& e) {
431 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
432 std_to_wx (e.what ())
436 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
439 /* This will terminate the program */
443 void OnUnhandledException ()
445 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
450 signal_manager->ui_idle ();