Tidying.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "content_advanced_dialog.h"
23 #include "content_menu.h"
24 #include "content_properties_dialog.h"
25 #include "repeat_dialog.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_video_content_view.h"
28 #include "wx_util.h"
29 #include "lib/audio_content.h"
30 #include "lib/config.h"
31 #include "lib/content_factory.h"
32 #include "lib/copy_dcp_details_to_film.h"
33 #include "lib/dcp_content.h"
34 #include "lib/dcp_examiner.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/exceptions.h"
37 #include "lib/ffmpeg_content.h"
38 #include "lib/film.h"
39 #include "lib/image_content.h"
40 #include "lib/job_manager.h"
41 #include "lib/playlist.h"
42 #include <dcp/cpl.h>
43 #include <dcp/decrypted_kdm.h>
44 #include <dcp/exceptions.h>
45 #include <dcp/search.h>
46 #include <wx/dirdlg.h>
47 #include <wx/wx.h>
48
49
50 using std::dynamic_pointer_cast;
51 using std::exception;
52 using std::list;
53 using std::make_shared;
54 using std::shared_ptr;
55 using std::vector;
56 using std::weak_ptr;
57 using boost::optional;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
60 #endif
61
62
63 enum {
64         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
65         ID_repeat = 256,
66         ID_join,
67         ID_find_missing,
68         ID_properties,
69         ID_advanced,
70         ID_re_examine,
71         ID_kdm,
72         ID_ov,
73         ID_choose_cpl,
74         ID_set_dcp_settings,
75         ID_remove
76 };
77
78
79 ContentMenu::ContentMenu (wxWindow* p)
80         : _menu (new wxMenu)
81         , _parent (p)
82         , _pop_up_open (false)
83 {
84         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
85         _join = _menu->Append (ID_join, _("Join"));
86         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
87         _properties = _menu->Append (ID_properties, _("Properties..."));
88         _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
89         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
90         _menu->AppendSeparator ();
91         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
92         _ov = _menu->Append (ID_ov, _("Add OV..."));
93         _cpl_menu = new wxMenu ();
94         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
95         _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
96         _menu->AppendSeparator ();
97         _remove = _menu->Append (ID_remove, _("Remove"));
98
99         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
100         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
101         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
102         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
103         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
104         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
105         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
106         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
107         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
108         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
109         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
110 }
111
112 void
113 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
114 {
115         _film = film;
116         _content = c;
117         _views = v;
118
119         int const N = _cpl_menu->GetMenuItemCount();
120         for (int i = 1; i <= N; ++i) {
121                 _cpl_menu->Delete (i);
122         }
123
124         _repeat->Enable (!_content.empty ());
125
126         int n = 0;
127         for (auto i: _content) {
128                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
129                         ++n;
130                 }
131         }
132
133         _join->Enable (n > 1);
134
135         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
136         _properties->Enable (_content.size() == 1);
137         _advanced->Enable (_content.size() == 1);
138         _re_examine->Enable (!_content.empty ());
139
140         if (_content.size() == 1) {
141                 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
142                 if (dcp) {
143                         _kdm->Enable (dcp->encrypted ());
144                         _ov->Enable (dcp->needs_assets ());
145                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
146                         try {
147                                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
148                                 _choose_cpl->Enable (cpls.size() > 1);
149                                 /* We can't have 0 as a menu item ID on OS X */
150                                 int id = 1;
151                                 for (auto i: cpls) {
152                                         auto item = _cpl_menu->AppendRadioItem (
153                                                 id++,
154                                                 wxString::Format (
155                                                         "%s (%s)",
156                                                         std_to_wx(i->annotation_text().get_value_or("")).data(),
157                                                         std_to_wx(i->id()).data()
158                                                         )
159                                                 );
160                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
161                                 }
162                         } catch (dcp::ReadError &) {
163                                 /* The DCP is probably missing */
164                         } catch (dcp::KDMDecryptionError &) {
165                                 /* We have an incorrect KDM */
166                         } catch (KDMError &) {
167                                 /* We have an incorrect KDM */
168                         }
169                 } else {
170                         _kdm->Enable (false);
171                         _ov->Enable (false);
172                         _choose_cpl->Enable (false);
173                         _set_dcp_settings->Enable (false);
174                 }
175         } else {
176                 _kdm->Enable (false);
177                 _set_dcp_settings->Enable (false);
178         }
179
180         _remove->Enable (!_content.empty ());
181
182         _pop_up_open = true;
183         _parent->PopupMenu (_menu, p);
184         _pop_up_open = false;
185 }
186
187
188 void
189 ContentMenu::set_dcp_settings ()
190 {
191         auto film = _film.lock ();
192         if (!film) {
193                 return;
194         }
195
196         DCPOMATIC_ASSERT (_content.size() == 1);
197         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
198         DCPOMATIC_ASSERT (dcp);
199         copy_dcp_details_to_film (dcp, film);
200 }
201
202
203 void
204 ContentMenu::repeat ()
205 {
206         if (_content.empty ()) {
207                 return;
208         }
209
210         auto d = new RepeatDialog (_parent);
211         if (d->ShowModal() != wxID_OK) {
212                 d->Destroy ();
213                 return;
214         }
215
216         auto film = _film.lock ();
217         if (!film) {
218                 return;
219         }
220
221         film->repeat_content (_content, d->number ());
222         d->Destroy ();
223
224         _content.clear ();
225         _views.clear ();
226 }
227
228
229 void
230 ContentMenu::join ()
231 {
232         vector<shared_ptr<Content>> fc;
233         for (auto i: _content) {
234                 auto f = dynamic_pointer_cast<FFmpegContent> (i);
235                 if (f) {
236                         fc.push_back (f);
237                 }
238         }
239
240         DCPOMATIC_ASSERT (fc.size() > 1);
241
242         auto film = _film.lock ();
243         if (!film) {
244                 return;
245         }
246
247         try {
248                 auto joined = make_shared<FFmpegContent>(fc);
249                 film->remove_content (_content);
250                 film->examine_and_add_content (joined);
251         } catch (JoinError& e) {
252                 error_dialog (_parent, std_to_wx (e.what ()));
253         }
254 }
255
256
257 void
258 ContentMenu::remove ()
259 {
260         if (_content.empty ()) {
261                 return;
262         }
263
264         auto film = _film.lock ();
265         if (!film) {
266                 return;
267         }
268
269         /* We are removing from the timeline if _views is not empty */
270         bool handled = false;
271         if (!_views.empty ()) {
272                 /* Special case: we only remove FFmpegContent if its video view is selected;
273                    if not, and its audio view is selected, we unmap the audio.
274                 */
275                 for (auto i: _content) {
276                         auto fc = dynamic_pointer_cast<FFmpegContent> (i);
277                         if (!fc) {
278                                 continue;
279                         }
280
281                         shared_ptr<TimelineVideoContentView> video;
282                         shared_ptr<TimelineAudioContentView> audio;
283
284                         for (auto j: _views) {
285                                 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
286                                 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
287                                 if (v && v->content() == fc) {
288                                         video = v;
289                                 } else if (a && a->content() == fc) {
290                                         audio = a;
291                                 }
292                         }
293
294                         if (!video && audio) {
295                                 auto m = fc->audio->mapping ();
296                                 m.unmap_all ();
297                                 fc->audio->set_mapping (m);
298                                 handled = true;
299                         }
300                 }
301         }
302
303         if (!handled) {
304                 film->remove_content (_content);
305         }
306
307         _content.clear ();
308         _views.clear ();
309 }
310
311
312 void
313 ContentMenu::find_missing ()
314 {
315         if (_content.size() != 1) {
316                 return;
317         }
318
319         auto film = _film.lock ();
320         if (!film) {
321                 return;
322         }
323
324         /* XXX: a bit nasty */
325         auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
326         auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
327
328         int r = wxID_CANCEL;
329         boost::filesystem::path path;
330
331         if ((ic && !ic->still ()) || dc) {
332                 auto d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
333                 r = d->ShowModal ();
334                 path = wx_to_std (d->GetPath ());
335                 d->Destroy ();
336         } else {
337                 auto d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
338                 r = d->ShowModal ();
339                 path = wx_to_std (d->GetPath ());
340                 d->Destroy ();
341         }
342
343         list<shared_ptr<Content>> content;
344
345         if (r == wxID_OK) {
346                 if (dc) {
347                         content.push_back (make_shared<DCPContent>(path));
348                 } else {
349                         content = content_factory (path);
350                 }
351         }
352
353         if (content.empty ()) {
354                 return;
355         }
356
357         for (auto i: content) {
358                 auto j = make_shared<ExamineContentJob>(film, i);
359
360                 j->Finished.connect (
361                         bind (
362                                 &ContentMenu::maybe_found_missing,
363                                 this,
364                                 std::weak_ptr<Job> (j),
365                                 std::weak_ptr<Content> (_content.front ()),
366                                 std::weak_ptr<Content> (i)
367                                 )
368                         );
369
370                 JobManager::instance()->add (j);
371         }
372 }
373
374 void
375 ContentMenu::re_examine ()
376 {
377         auto film = _film.lock ();
378         if (!film) {
379                 return;
380         }
381
382         for (auto i: _content) {
383                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
384         }
385 }
386
387 void
388 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
389 {
390         auto job = j.lock ();
391         if (!job || !job->finished_ok ()) {
392                 return;
393         }
394
395         auto old_content = oc.lock ();
396         auto new_content = nc.lock ();
397         DCPOMATIC_ASSERT (old_content);
398         DCPOMATIC_ASSERT (new_content);
399
400         if (new_content->digest() != old_content->digest()) {
401                 error_dialog (0, _("The content file(s) you specified are not the same as those that are missing.  Either try again with the correct content file or remove the missing content."));
402                 return;
403         }
404
405         old_content->set_paths (new_content->paths());
406 }
407
408 void
409 ContentMenu::kdm ()
410 {
411         DCPOMATIC_ASSERT (!_content.empty ());
412         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
413         DCPOMATIC_ASSERT (dcp);
414
415         auto d = new wxFileDialog (_parent, _("Select KDM"));
416
417         if (d->ShowModal() == wxID_OK) {
418                 optional<dcp::EncryptedKDM> kdm;
419                 try {
420                         kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
421                 } catch (exception& e) {
422                         error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
423                         d->Destroy ();
424                         return;
425                 }
426
427                 /* Try to decrypt it to get an early preview of any errors */
428                 try {
429                         decrypt_kdm_with_helpful_error (*kdm);
430                 } catch (KDMError& e) {
431                         error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
432                         return;
433                 } catch (exception& e) {
434                         error_dialog (_parent, e.what());
435                         return;
436                 }
437
438                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
439                 bool const kdm_matches_any_cpl = std::any_of(cpls.begin(), cpls.end(), [kdm](shared_ptr<const dcp::CPL> cpl) { return cpl->id() == kdm->cpl_id(); });
440                 bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
441
442                 if (!kdm_matches_any_cpl) {
443                         error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
444                         return;
445                 }
446
447                 if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
448                         message_dialog (_parent, _("This KDM was made for one of the CPLs in this DCP, but not the currently selected one.  To play the currently-selected CPL you will need a different KDM."));
449                 }
450
451                 dcp->add_kdm (*kdm);
452
453                 auto film = _film.lock ();
454                 DCPOMATIC_ASSERT (film);
455                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
456         }
457
458         d->Destroy ();
459 }
460
461 void
462 ContentMenu::ov ()
463 {
464         DCPOMATIC_ASSERT (!_content.empty ());
465         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
466         DCPOMATIC_ASSERT (dcp);
467
468         auto d = new wxDirDialog (_parent, _("Select OV"));
469
470         if (d->ShowModal() == wxID_OK) {
471                 dcp->add_ov (wx_to_std (d->GetPath()));
472                 shared_ptr<Film> film = _film.lock ();
473                 DCPOMATIC_ASSERT (film);
474                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
475         }
476
477         d->Destroy ();
478 }
479
480 void
481 ContentMenu::properties ()
482 {
483         auto film = _film.lock ();
484         DCPOMATIC_ASSERT (film);
485         auto d = new ContentPropertiesDialog (_parent, film, _content.front());
486         d->ShowModal ();
487         d->Destroy ();
488 }
489
490
491 void
492 ContentMenu::advanced ()
493 {
494         auto d = new ContentAdvancedDialog (_parent, _content.front());
495         d->ShowModal ();
496         d->Destroy ();
497 }
498
499
500 void
501 ContentMenu::cpl_selected (wxCommandEvent& ev)
502 {
503         if (!_pop_up_open) {
504                 return;
505         }
506
507         DCPOMATIC_ASSERT (!_content.empty ());
508         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
509         DCPOMATIC_ASSERT (dcp);
510
511         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
512         DCPOMATIC_ASSERT (ev.GetId() > 0);
513         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
514
515         auto i = cpls.begin ();
516         for (int j = 0; j < ev.GetId() - 1; ++j) {
517                 ++i;
518         }
519
520         dcp->set_cpl ((*i)->id ());
521         auto film = _film.lock ();
522         DCPOMATIC_ASSERT (film);
523         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
524 }