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 "wx/file_dialog_wrapper.h"
31 #include "wx/editable_list.h"
32 #include "lib/config.h"
34 #include "lib/screen.h"
35 #include "lib/job_manager.h"
36 #include "lib/screen_kdm.h"
37 #include "lib/exceptions.h"
38 #include "lib/cinema_kdms.h"
39 #include "lib/send_kdm_email_job.h"
40 #include "lib/compose.hpp"
41 #include <dcp/encrypted_kdm.h>
42 #include <dcp/decrypted_kdm.h>
43 #include <dcp/exceptions.h>
45 #include <wx/preferences.h>
46 #include <wx/filepicker.h>
48 #include <ApplicationServices/ApplicationServices.h>
50 #include <boost/bind.hpp>
51 #include <boost/foreach.hpp>
61 using boost::shared_ptr;
65 ID_help_report_a_problem = 1,
68 class KDMFileDialogWrapper : public FileDialogWrapper<dcp::EncryptedKDM>
71 KDMFileDialogWrapper (wxWindow* parent)
72 : FileDialogWrapper<dcp::EncryptedKDM> (parent, _("Select DKDM file"))
79 column (dcp::EncryptedKDM k)
81 return String::compose ("%1 (%2)", k.content_title_text(), k.cpl_id());
84 class DOMFrame : public wxFrame
87 DOMFrame (wxString const & title)
88 : wxFrame (NULL, -1, title)
92 #if defined(DCPOMATIC_WINDOWS)
93 if (Config::instance()->win32_console ()) {
96 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
97 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
98 FILE* hf_out = _fdopen(hCrt, "w");
99 setvbuf(hf_out, NULL, _IONBF, 1);
102 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
103 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
104 FILE* hf_in = _fdopen(hCrt, "r");
105 setvbuf(hf_in, NULL, _IONBF, 128);
108 std::cout << "DCP-o-matic KDM creator is starting." << "\n";
112 wxMenuBar* bar = new wxMenuBar;
116 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
117 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
118 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
119 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
121 /* Use a panel as the only child of the Frame so that we avoid
122 the dark-grey background on Windows.
124 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
125 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
127 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
129 wxFont subheading_font (*wxNORMAL_FONT);
130 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
132 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
133 h->SetFont (subheading_font);
134 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
135 _screens = new ScreensPanel (overall_panel);
136 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
138 _timing = new KDMTimingPanel (overall_panel);
139 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
141 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
142 h->SetFont (subheading_font);
143 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
145 vector<string> columns;
146 columns.push_back (wx_to_std (_("CPL")));
147 _dkdm = new EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper> (
148 overall_panel, columns, bind (&DOMFrame::dkdms, this), bind (&DOMFrame::set_dkdms, this, _1), bind (&column, _1), false
150 vertical->Add (_dkdm, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
152 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
153 h->SetFont (subheading_font);
154 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
155 /* XXX: hard-coded non-interop here */
156 _output = new KDMOutputPanel (overall_panel, false);
157 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
159 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
160 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
162 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
163 overall_panel->SetSizer (main_sizer);
165 /* Instantly save any config changes when using a DCP-o-matic GUI */
166 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
168 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
169 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
170 _dkdm->SelectionChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
172 setup_sensitivity ();
176 vector<dcp::EncryptedKDM> dkdms () const
178 return Config::instance()->dkdms ();
181 void set_dkdms (vector<dcp::EncryptedKDM> dkdms)
183 Config::instance()->set_dkdms (dkdms);
188 /* false here allows the close handler to veto the close request */
192 void edit_preferences ()
194 if (!_config_dialog) {
195 _config_dialog = create_config_dialog ();
197 _config_dialog->Show (this);
202 AboutDialog* d = new AboutDialog (this);
207 void help_report_a_problem ()
209 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
210 if (d->ShowModal () == wxID_OK) {
216 void setup_menu (wxMenuBar* m)
218 wxMenu* file = new wxMenu;
221 file->Append (wxID_EXIT, _("&Exit"));
223 file->Append (wxID_EXIT, _("&Quit"));
227 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
229 wxMenu* edit = new wxMenu;
230 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
233 wxMenu* help = new wxMenu;
235 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
237 help->Append (wxID_ABOUT, _("About"));
239 help->Append (ID_help_report_a_problem, _("Report a problem..."));
241 m->Append (file, _("&File"));
243 m->Append (edit, _("&Edit"));
245 m->Append (help, _("&Help"));
251 if (!_dkdm->selection()) {
255 /* Decrypt the DKDM */
256 dcp::DecryptedKDM decrypted (_dkdm->selection().get(), 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->selection());
330 wxPreferencesEditor* _config_dialog;
331 ScreensPanel* _screens;
332 KDMTimingPanel* _timing;
333 EditableList<dcp::EncryptedKDM, KDMFileDialogWrapper>* _dkdm;
335 KDMOutputPanel* _output;
336 JobViewDialog* _job_view;
340 * @brief The magic App class for wxWidgets.
342 class App : public wxApp
355 wxInitAllImageHandlers ();
357 SetAppName (_("DCP-o-matic KDM creator"));
359 if (!wxApp::OnInit()) {
363 #ifdef DCPOMATIC_LINUX
364 unsetenv ("UBUNTU_MENUPROXY");
368 ProcessSerialNumber serial;
369 GetCurrentProcess (&serial);
370 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
373 dcpomatic_setup_path_encoding ();
375 /* Enable i18n; this will create a Config object
376 to look for a force-configured language. This Config
377 object will be wrong, however, because dcpomatic_setup
378 hasn't yet been called and there aren't any filters etc.
381 dcpomatic_setup_i18n ();
383 /* Set things up, including filters etc.
384 which will now be internationalised correctly.
388 /* Force the configuration to be re-loaded correctly next
393 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
394 SetTopWindow (_frame);
398 signal_manager = new wxSignalManager (this);
399 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
405 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
409 /* An unhandled exception has occurred inside the main event loop */
410 bool OnExceptionInMainLoop ()
414 } catch (FileError& e) {
418 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
419 std_to_wx (e.what()),
420 std_to_wx (e.file().string().c_str ())
423 } catch (exception& e) {
427 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
428 std_to_wx (e.what ())
432 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
435 /* This will terminate the program */
439 void OnUnhandledException ()
441 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
446 signal_manager->ui_idle ();