2 Copyright (C) 2013-2020 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 "content_menu.h"
22 #include "repeat_dialog.h"
24 #include "timeline_video_content_view.h"
25 #include "timeline_audio_content_view.h"
26 #include "content_properties_dialog.h"
27 #include "content_advanced_dialog.h"
28 #include "lib/playlist.h"
30 #include "lib/image_content.h"
31 #include "lib/content_factory.h"
32 #include "lib/examine_content_job.h"
33 #include "lib/job_manager.h"
34 #include "lib/exceptions.h"
35 #include "lib/dcp_content.h"
36 #include "lib/dcp_examiner.h"
37 #include "lib/ffmpeg_content.h"
38 #include "lib/audio_content.h"
39 #include "lib/config.h"
40 #include "lib/copy_dcp_details_to_film.h"
42 #include <dcp/exceptions.h>
43 #include <dcp/decrypted_kdm.h>
45 #include <wx/dirdlg.h>
52 using std::shared_ptr;
54 using std::dynamic_pointer_cast;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
62 /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
76 ContentMenu::ContentMenu (wxWindow* p)
79 , _pop_up_open (false)
81 _repeat = _menu->Append (ID_repeat, _("Repeat..."));
82 _join = _menu->Append (ID_join, _("Join"));
83 _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
84 _properties = _menu->Append (ID_properties, _("Properties..."));
85 _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
86 _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
87 _menu->AppendSeparator ();
88 _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
89 _ov = _menu->Append (ID_ov, _("Add OV..."));
90 _cpl_menu = new wxMenu ();
91 _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
92 _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
93 _menu->AppendSeparator ();
94 _remove = _menu->Append (ID_remove, _("Remove"));
96 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
97 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
98 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
99 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
100 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
101 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
102 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
103 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
104 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
105 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
106 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
110 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
116 int const N = _cpl_menu->GetMenuItemCount();
117 for (int i = 1; i <= N; ++i) {
118 _cpl_menu->Delete (i);
121 _repeat->Enable (!_content.empty ());
124 for (auto i: _content) {
125 if (dynamic_pointer_cast<FFmpegContent> (i)) {
130 _join->Enable (n > 1);
132 _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
133 _properties->Enable (_content.size() == 1);
134 _advanced->Enable (_content.size() == 1);
135 _re_examine->Enable (!_content.empty ());
137 if (_content.size() == 1) {
138 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
140 _kdm->Enable (dcp->encrypted ());
141 _ov->Enable (dcp->needs_assets ());
142 _set_dcp_settings->Enable (static_cast<bool>(dcp));
144 DCPExaminer ex (dcp, true);
145 list<shared_ptr<dcp::CPL> > cpls = ex.cpls ();
146 _choose_cpl->Enable (cpls.size() > 1);
147 /* We can't have 0 as a menu item ID on OS X */
150 wxMenuItem* item = _cpl_menu->AppendRadioItem (
154 std_to_wx(i->annotation_text()).data(),
155 std_to_wx(i->id()).data()
158 item->Check (dcp->cpl() && dcp->cpl() == i->id());
160 } catch (dcp::ReadError &) {
161 /* The DCP is probably missing */
162 } catch (dcp::KDMDecryptionError &) {
163 /* We have an incorrect KDM */
164 } catch (KDMError &) {
165 /* We have an incorrect KDM */
168 _kdm->Enable (false);
170 _choose_cpl->Enable (false);
171 _set_dcp_settings->Enable (false);
174 _kdm->Enable (false);
175 _set_dcp_settings->Enable (false);
178 _remove->Enable (!_content.empty ());
181 _parent->PopupMenu (_menu, p);
182 _pop_up_open = false;
187 ContentMenu::set_dcp_settings ()
189 shared_ptr<Film> film = _film.lock ();
194 DCPOMATIC_ASSERT (_content.size() == 1);
195 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(_content.front());
196 DCPOMATIC_ASSERT (dcp);
197 copy_dcp_details_to_film (dcp, film);
202 ContentMenu::repeat ()
204 if (_content.empty ()) {
208 RepeatDialog* d = new RepeatDialog (_parent);
209 if (d->ShowModal() != wxID_OK) {
214 shared_ptr<Film> film = _film.lock ();
219 film->repeat_content (_content, d->number ());
229 vector<shared_ptr<Content> > fc;
230 for (auto i: _content) {
231 shared_ptr<FFmpegContent> f = dynamic_pointer_cast<FFmpegContent> (i);
237 DCPOMATIC_ASSERT (fc.size() > 1);
239 shared_ptr<Film> film = _film.lock ();
245 shared_ptr<FFmpegContent> joined (new FFmpegContent(fc));
246 film->remove_content (_content);
247 film->examine_and_add_content (joined);
248 } catch (JoinError& e) {
249 error_dialog (_parent, std_to_wx (e.what ()));
254 ContentMenu::remove ()
256 if (_content.empty ()) {
260 shared_ptr<Film> film = _film.lock ();
265 /* We are removing from the timeline if _views is not empty */
266 bool handled = false;
267 if (!_views.empty ()) {
268 /* Special case: we only remove FFmpegContent if its video view is selected;
269 if not, and its audio view is selected, we unmap the audio.
271 for (auto i: _content) {
272 shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i);
277 shared_ptr<TimelineVideoContentView> video;
278 shared_ptr<TimelineAudioContentView> audio;
280 for (auto j: _views) {
281 shared_ptr<TimelineVideoContentView> v = dynamic_pointer_cast<TimelineVideoContentView> (j);
282 shared_ptr<TimelineAudioContentView> a = dynamic_pointer_cast<TimelineAudioContentView> (j);
283 if (v && v->content() == fc) {
285 } else if (a && a->content() == fc) {
290 if (!video && audio) {
291 AudioMapping m = fc->audio->mapping ();
293 fc->audio->set_mapping (m);
300 film->remove_content (_content);
308 ContentMenu::find_missing ()
310 if (_content.size() != 1) {
314 shared_ptr<const Film> film = _film.lock ();
319 /* XXX: a bit nasty */
320 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (_content.front ());
321 shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (_content.front ());
324 boost::filesystem::path path;
326 if ((ic && !ic->still ()) || dc) {
327 wxDirDialog* d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
329 path = wx_to_std (d->GetPath ());
332 wxFileDialog* d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
334 path = wx_to_std (d->GetPath ());
338 list<shared_ptr<Content> > content;
342 content.push_back (shared_ptr<DCPContent>(new DCPContent(path)));
344 content = content_factory (path);
348 if (content.empty ()) {
352 for (auto i: content) {
353 shared_ptr<Job> j (new ExamineContentJob (film, i));
355 j->Finished.connect (
357 &ContentMenu::maybe_found_missing,
359 std::weak_ptr<Job> (j),
360 std::weak_ptr<Content> (_content.front ()),
361 std::weak_ptr<Content> (i)
365 JobManager::instance()->add (j);
370 ContentMenu::re_examine ()
372 shared_ptr<Film> film = _film.lock ();
377 for (auto i: _content) {
378 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
383 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
385 shared_ptr<Job> job = j.lock ();
386 if (!job || !job->finished_ok ()) {
390 shared_ptr<Content> old_content = oc.lock ();
391 shared_ptr<Content> new_content = nc.lock ();
392 DCPOMATIC_ASSERT (old_content);
393 DCPOMATIC_ASSERT (new_content);
395 if (new_content->digest() != old_content->digest()) {
396 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."));
400 old_content->set_paths (new_content->paths());
406 DCPOMATIC_ASSERT (!_content.empty ());
407 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
408 DCPOMATIC_ASSERT (dcp);
410 wxFileDialog* d = new wxFileDialog (_parent, _("Select KDM"));
412 if (d->ShowModal() == wxID_OK) {
413 optional<dcp::EncryptedKDM> kdm;
415 kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
416 } catch (exception& e) {
417 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
422 /* Try to decrypt it to get an early preview of any errors */
424 decrypt_kdm_with_helpful_error (*kdm);
425 } catch (KDMError& e) {
426 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
428 } catch (exception& e) {
429 error_dialog (_parent, e.what());
433 DCPExaminer ex (dcp, true);
435 bool kdm_matches_any_cpl = false;
436 for (auto i: ex.cpls()) {
437 if (i->id() == kdm->cpl_id()) {
438 kdm_matches_any_cpl = true;
442 bool kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
444 if (!kdm_matches_any_cpl) {
445 error_dialog (_parent, _("This KDM was not made for this DCP. You will need a different one."));
449 if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
450 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."));
455 shared_ptr<Film> film = _film.lock ();
456 DCPOMATIC_ASSERT (film);
457 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
466 DCPOMATIC_ASSERT (!_content.empty ());
467 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
468 DCPOMATIC_ASSERT (dcp);
470 wxDirDialog* d = new wxDirDialog (_parent, _("Select OV"));
472 if (d->ShowModal() == wxID_OK) {
473 dcp->add_ov (wx_to_std (d->GetPath ()));
474 shared_ptr<Film> film = _film.lock ();
475 DCPOMATIC_ASSERT (film);
476 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
483 ContentMenu::properties ()
485 shared_ptr<Film> film = _film.lock ();
486 DCPOMATIC_ASSERT (film);
487 ContentPropertiesDialog* d = new ContentPropertiesDialog (_parent, film, _content.front());
494 ContentMenu::advanced ()
496 ContentAdvancedDialog* d = new ContentAdvancedDialog (_parent, _content.front());
503 ContentMenu::cpl_selected (wxCommandEvent& ev)
509 DCPOMATIC_ASSERT (!_content.empty ());
510 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
511 DCPOMATIC_ASSERT (dcp);
513 DCPExaminer ex (dcp, true);
514 list<shared_ptr<dcp::CPL> > cpls = ex.cpls ();
515 DCPOMATIC_ASSERT (ev.GetId() > 0);
516 DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
518 list<shared_ptr<dcp::CPL> >::const_iterator i = cpls.begin ();
519 for (int j = 0; j < ev.GetId() - 1; ++j) {
523 dcp->set_cpl ((*i)->id ());
524 shared_ptr<Film> film = _film.lock ();
525 DCPOMATIC_ASSERT (film);
526 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));