Shell of KDM GUI tool.
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "wx/config_dialog.h"
21 #include "wx/about_dialog.h"
22 #include "wx/report_problem_dialog.h"
23 #include "wx/wx_util.h"
24 #include "wx/wx_signal_manager.h"
25 #include "wx/screens_panel.h"
26 #include "wx/kdm_timing_panel.h"
27 #include "wx/kdm_output_panel.h"
28 #include "lib/config.h"
29 #include "lib/util.h"
30 #include <dcp/encrypted_kdm.h>
31 #include <dcp/decrypted_kdm.h>
32 #include <wx/wx.h>
33 #include <wx/preferences.h>
34 #include <wx/filepicker.h>
35 #include <boost/bind.hpp>
36
37 #ifdef check
38 #undef check
39 #endif
40
41 using std::exception;
42 using boost::shared_ptr;
43 using boost::bind;
44
45 enum {
46         ID_help_report_a_problem,
47 };
48
49 class DOMFrame : public wxFrame
50 {
51 public:
52         DOMFrame (wxString const & title)
53                 : wxFrame (NULL, -1, title)
54                 , _config_dialog (0)
55         {
56                 wxMenuBar* bar = new wxMenuBar;
57                 setup_menu (bar);
58                 SetMenuBar (bar);
59
60                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_exit, this),             wxID_EXIT);
61                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this),      wxID_PREFERENCES);
62                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this),            wxID_ABOUT);
63                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
64
65                 /* Use a panel as the only child of the Frame so that we avoid
66                    the dark-grey background on Windows.
67                 */
68                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
69                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
70
71                 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
72
73                 wxFont subheading_font (*wxNORMAL_FONT);
74                 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
75
76                 wxStaticText* h = new wxStaticText (overall_panel, wxID_ANY, _("Screens"));
77                 h->SetFont (subheading_font);
78                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
79                 _screens = new ScreensPanel (overall_panel);
80                 vertical->Add (_screens, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
81
82                 h = new wxStaticText (overall_panel, wxID_ANY, S_("KDM|Timing"));
83                 h->SetFont (subheading_font);
84                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
85                 _timing = new KDMTimingPanel (overall_panel);
86                 vertical->Add (_timing, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
87
88                 h = new wxStaticText (overall_panel, wxID_ANY, _("DKDM"));
89                 h->SetFont (subheading_font);
90                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
91                 wxSizer* dkdm = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
92                 add_label_to_sizer (dkdm, overall_panel, _("DKDM file"), true);
93 #ifdef DCPOMATIC_USE_OWN_PICKER
94                 _dkdm = new FilePicker (overall_panel, _("Select a DKDM XML file..."), "*.xml");
95 #else
96                 _dkdm = new wxFilePickerCtrl (overall_panel, wxID_ANY, wxEmptyString, _("Select a DKDM XML file..."), "*.xml", wxDefaultPosition, wxSize (300, -1));
97 #endif
98                 dkdm->Add (_dkdm, 1, wxEXPAND);
99                 add_label_to_sizer (dkdm, overall_panel, _("Annotation"), true);
100                 _annotation_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
101                 dkdm->Add (_annotation_text, 1, wxEXPAND);
102                 add_label_to_sizer (dkdm, overall_panel, _("Content title"), true);
103                 _content_title_text = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
104                 dkdm->Add (_content_title_text, 1, wxEXPAND);
105                 add_label_to_sizer (dkdm, overall_panel, _("Issue date"), true);
106                 _issue_date = new wxStaticText (overall_panel, wxID_ANY, wxT(""));
107                 dkdm->Add (_issue_date, 1, wxEXPAND);
108                 vertical->Add (dkdm, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
109
110                 h = new wxStaticText (overall_panel, wxID_ANY, _("Output"));
111                 h->SetFont (subheading_font);
112                 vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
113                 /* XXX: hard-coded non-interop here */
114                 _output = new KDMOutputPanel (overall_panel, false);
115                 vertical->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
116
117                 _create = new wxButton (overall_panel, wxID_ANY, _("Create KDMs"));
118                 vertical->Add (_create, 0, wxALL, DCPOMATIC_SIZER_GAP);
119
120                 main_sizer->Add (vertical, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
121                 overall_panel->SetSizer (main_sizer);
122
123                 /* Instantly save any config changes when using a DCP-o-matic GUI */
124                 Config::instance()->Changed.connect (boost::bind (&Config::write, Config::instance ()));
125
126                 _screens->ScreensChanged.connect (boost::bind (&DOMFrame::setup_sensitivity, this));
127                 _dkdm->Bind (wxEVT_COMMAND_FILEPICKER_CHANGED, bind (&DOMFrame::dkdm_changed, this));
128                 _create->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&DOMFrame::create_kdms, this));
129         }
130
131 private:
132         void file_exit ()
133         {
134                 /* false here allows the close handler to veto the close request */
135                 Close (false);
136         }
137
138         void edit_preferences ()
139         {
140                 if (!_config_dialog) {
141                         _config_dialog = create_config_dialog ();
142                 }
143                 _config_dialog->Show (this);
144         }
145
146         void help_about ()
147         {
148                 AboutDialog* d = new AboutDialog (this);
149                 d->ShowModal ();
150                 d->Destroy ();
151         }
152
153         void help_report_a_problem ()
154         {
155                 ReportProblemDialog* d = new ReportProblemDialog (this, shared_ptr<Film> ());
156                 if (d->ShowModal () == wxID_OK) {
157                         d->report ();
158                 }
159                 d->Destroy ();
160         }
161
162         void setup_menu (wxMenuBar* m)
163         {
164                 wxMenu* file = new wxMenu;
165
166 #ifdef __WXOSX__
167                 file->Append (wxID_EXIT, _("&Exit"));
168 #else
169                 file->Append (wxID_EXIT, _("&Quit"));
170 #endif
171
172 #ifdef __WXOSX__
173                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
174 #else
175                 wxMenu* edit = new wxMenu;
176                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
177 #endif
178
179                 wxMenu* help = new wxMenu;
180 #ifdef __WXOSX__
181                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
182 #else
183                 help->Append (wxID_ABOUT, _("About"));
184 #endif
185                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
186
187                 m->Append (file, _("&File"));
188 #ifndef __WXOSX__
189                 m->Append (edit, _("&Edit"));
190 #endif
191                 m->Append (help, _("&Help"));
192         }
193
194         void dkdm_changed ()
195         {
196                 try {
197                         dcp::EncryptedKDM encrypted (dcp::file_to_string (wx_to_std (_dkdm->GetPath())));
198                         dcp::DecryptedKDM decrypted (encrypted, Config::instance()->decryption_chain()->key().get());
199                         _annotation_text->Enable (true);
200                         _annotation_text->SetLabel (std_to_wx (decrypted.annotation_text ()));
201                         _content_title_text->Enable (true);
202                         _content_title_text->SetLabel (std_to_wx (decrypted.content_title_text ()));
203                         _issue_date->Enable (true);
204                         _issue_date->SetLabel (std_to_wx (decrypted.issue_date ()));
205                 } catch (exception& e) {
206                         error_dialog (this, wxString::Format (_("Could not load DKDM (%s)"), std_to_wx (e.what()).data()));
207                         _dkdm->SetPath (wxT(""));
208                         _annotation_text->SetLabel (wxT(""));
209                         _annotation_text->Enable (false);
210                         _content_title_text->SetLabel (wxT(""));
211                         _content_title_text->Enable (false);
212                         _issue_date->SetLabel (wxT(""));
213                         _issue_date->Enable (false);
214                 }
215         }
216
217         void create_kdms ()
218         {
219 #if 0
220                 try {
221                         if (_output->write_to()) {
222                                 write_kdm_files (
223                                         _film, d->screens (), d->cpl(), _timing->from(), _timing->until(), _output->formulation(), _output->directory()
224                                         );
225                         } else {
226                                 JobManager::instance()->add (
227                                         shared_ptr<Job> (new SendKDMEmailJob (_film, d->screens (), d->cpl (), d->from (), d->until (), d->formulation ()))
228                                         );
229                         }
230                 } catch (dcp::NotEncryptedError& e) {
231                         error_dialog (this, _("CPL's content is not encrypted."));
232                 } catch (exception& e) {
233                         error_dialog (this, e.what ());
234                 } catch (...) {
235                         error_dialog (this, _("An unknown exception occurred."));
236                 }
237 #endif
238         }
239
240         void setup_sensitivity ()
241         {
242                 _screens->setup_sensitivity ();
243                 _output->setup_sensitivity ();
244                 _create->Enable (!_screens->screens().empty());
245         }
246
247         wxPreferencesEditor* _config_dialog;
248         ScreensPanel* _screens;
249         KDMTimingPanel* _timing;
250 #ifdef DCPOMATIC_USE_OWN_PICKER
251         FilePicker* _dkdm;
252 #else
253         wxFilePickerCtrl* _dkdm;
254 #endif
255         wxStaticText* _annotation_text;
256         wxStaticText* _content_title_text;
257         wxStaticText* _issue_date;
258         wxButton* _create;
259         KDMOutputPanel* _output;
260 };
261
262 /** @class App
263  *  @brief The magic App class for wxWidgets.
264  */
265 class App : public wxApp
266 {
267 public:
268         App ()
269                 : wxApp ()
270                 , _frame (0)
271         {}
272
273 private:
274
275         bool OnInit ()
276         try
277         {
278                 wxInitAllImageHandlers ();
279
280                 SetAppName (_("DCP-o-matic KDM creator"));
281
282                 if (!wxApp::OnInit()) {
283                         return false;
284                 }
285
286 #ifdef DCPOMATIC_LINUX
287                 unsetenv ("UBUNTU_MENUPROXY");
288 #endif
289
290 #ifdef __WXOSX__
291                 ProcessSerialNumber serial;
292                 GetCurrentProcess (&serial);
293                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
294 #endif
295
296                 dcpomatic_setup_path_encoding ();
297
298                 /* Enable i18n; this will create a Config object
299                    to look for a force-configured language.  This Config
300                    object will be wrong, however, because dcpomatic_setup
301                    hasn't yet been called and there aren't any filters etc.
302                    set up yet.
303                 */
304                 dcpomatic_setup_i18n ();
305
306                 /* Set things up, including filters etc.
307                    which will now be internationalised correctly.
308                 */
309                 dcpomatic_setup ();
310
311                 /* Force the configuration to be re-loaded correctly next
312                    time it is needed.
313                 */
314                 Config::drop ();
315
316                 _frame = new DOMFrame (_("DCP-o-matic KDM creator"));
317                 SetTopWindow (_frame);
318                 _frame->Maximize ();
319                 _frame->Show ();
320
321                 signal_manager = new wxSignalManager (this);
322                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
323
324                 return true;
325         }
326         catch (exception& e)
327         {
328                 error_dialog (0, wxString::Format ("DCP-o-matic could not start: %s", e.what ()));
329                 return true;
330         }
331
332         /* An unhandled exception has occurred inside the main event loop */
333         bool OnExceptionInMainLoop ()
334         {
335                 try {
336                         throw;
337                 } catch (FileError& e) {
338                         error_dialog (
339                                 0,
340                                 wxString::Format (
341                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
342                                         std_to_wx (e.what()),
343                                         std_to_wx (e.file().string().c_str ())
344                                         )
345                                 );
346                 } catch (exception& e) {
347                         error_dialog (
348                                 0,
349                                 wxString::Format (
350                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
351                                         std_to_wx (e.what ())
352                                         )
353                                 );
354                 } catch (...) {
355                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
356                 }
357
358                 /* This will terminate the program */
359                 return false;
360         }
361
362         void OnUnhandledException ()
363         {
364                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
365         }
366
367         void idle ()
368         {
369                 signal_manager->ui_idle ();
370         }
371
372         DOMFrame* _frame;
373 };
374
375 IMPLEMENT_APP (App)