2 Copyright (C) 2013-2021 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/>.
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"
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"
39 #include "lib/find_missing.h"
40 #include "lib/image_content.h"
41 #include "lib/job_manager.h"
42 #include "lib/playlist.h"
44 #include <dcp/decrypted_kdm.h>
45 #include <dcp/exceptions.h>
46 #include <wx/dirdlg.h>
52 using std::dynamic_pointer_cast;
55 using std::make_shared;
56 using std::shared_ptr;
59 using boost::optional;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
66 /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
81 ContentMenu::ContentMenu (wxWindow* p)
84 , _pop_up_open (false)
86 _repeat = _menu->Append (ID_repeat, _("Repeat..."));
87 _join = _menu->Append (ID_join, _("Join"));
88 _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
89 _properties = _menu->Append (ID_properties, _("Properties..."));
90 _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
91 _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
92 _menu->AppendSeparator ();
93 _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
94 _ov = _menu->Append (ID_ov, _("Add OV..."));
95 _cpl_menu = new wxMenu ();
96 _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
97 _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
98 _menu->AppendSeparator ();
99 _remove = _menu->Append (ID_remove, _("Remove"));
101 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
102 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
103 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
104 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
105 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
106 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
107 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
108 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
109 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
110 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
111 _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
115 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
121 int const N = _cpl_menu->GetMenuItemCount();
122 for (int i = 1; i <= N; ++i) {
123 _cpl_menu->Delete (i);
126 _repeat->Enable (!_content.empty ());
129 for (auto i: _content) {
130 if (dynamic_pointer_cast<FFmpegContent> (i)) {
135 _join->Enable (n > 1);
137 _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
138 _properties->Enable (_content.size() == 1);
139 _advanced->Enable (_content.size() == 1);
140 _re_examine->Enable (!_content.empty ());
142 if (_content.size() == 1) {
143 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
145 _kdm->Enable (dcp->encrypted ());
146 _ov->Enable (dcp->needs_assets ());
147 _set_dcp_settings->Enable (static_cast<bool>(dcp));
149 DCPExaminer ex (dcp, true);
150 auto cpls = ex.cpls ();
151 _choose_cpl->Enable (cpls.size() > 1);
152 /* We can't have 0 as a menu item ID on OS X */
155 auto item = _cpl_menu->AppendRadioItem (
159 std_to_wx(i->annotation_text().get_value_or("")).data(),
160 std_to_wx(i->id()).data()
163 item->Check (dcp->cpl() && dcp->cpl() == i->id());
165 } catch (dcp::ReadError &) {
166 /* The DCP is probably missing */
167 } catch (dcp::KDMDecryptionError &) {
168 /* We have an incorrect KDM */
169 } catch (KDMError &) {
170 /* We have an incorrect KDM */
173 _kdm->Enable (false);
175 _choose_cpl->Enable (false);
176 _set_dcp_settings->Enable (false);
179 _kdm->Enable (false);
180 _set_dcp_settings->Enable (false);
183 _remove->Enable (!_content.empty ());
186 _parent->PopupMenu (_menu, p);
187 _pop_up_open = false;
192 ContentMenu::set_dcp_settings ()
194 auto film = _film.lock ();
199 DCPOMATIC_ASSERT (_content.size() == 1);
200 auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
201 DCPOMATIC_ASSERT (dcp);
202 copy_dcp_details_to_film (dcp, film);
207 ContentMenu::repeat ()
209 if (_content.empty ()) {
213 auto d = new RepeatDialog (_parent);
214 if (d->ShowModal() != wxID_OK) {
219 auto film = _film.lock ();
224 film->repeat_content (_content, d->number ());
235 vector<shared_ptr<Content>> fc;
236 for (auto i: _content) {
237 auto f = dynamic_pointer_cast<FFmpegContent> (i);
243 DCPOMATIC_ASSERT (fc.size() > 1);
245 auto film = _film.lock ();
251 auto joined = make_shared<FFmpegContent>(fc);
252 film->remove_content (_content);
253 film->examine_and_add_content (joined);
254 } catch (JoinError& e) {
255 error_dialog (_parent, std_to_wx (e.what ()));
261 ContentMenu::remove ()
263 if (_content.empty ()) {
267 auto film = _film.lock ();
272 /* We are removing from the timeline if _views is not empty */
273 bool handled = false;
274 if (!_views.empty ()) {
275 /* Special case: we only remove FFmpegContent if its video view is selected;
276 if not, and its audio view is selected, we unmap the audio.
278 for (auto i: _content) {
279 auto fc = dynamic_pointer_cast<FFmpegContent> (i);
284 shared_ptr<TimelineVideoContentView> video;
285 shared_ptr<TimelineAudioContentView> audio;
287 for (auto j: _views) {
288 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
289 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
290 if (v && v->content() == fc) {
292 } else if (a && a->content() == fc) {
297 if (!video && audio) {
298 auto m = fc->audio->mapping ();
300 fc->audio->set_mapping (m);
307 film->remove_content (_content);
316 ContentMenu::find_missing ()
318 if (_content.size() != 1) {
322 auto film = _film.lock ();
327 /* XXX: a bit nasty */
328 auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
329 auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
332 boost::filesystem::path path;
334 if ((ic && !ic->still ()) || dc) {
335 auto d = new wxDirDialog (nullptr, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
337 path = wx_to_std (d->GetPath());
340 auto d = new wxFileDialog (nullptr, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
342 path = wx_to_std (d->GetPath());
346 list<shared_ptr<Content>> content;
350 content.push_back (make_shared<DCPContent>(path));
352 content = content_factory (path);
356 if (content.empty ()) {
360 for (auto i: content) {
361 auto j = make_shared<ExamineContentJob>(film, i);
363 j->Finished.connect (
365 &ContentMenu::maybe_found_missing,
367 std::weak_ptr<Job> (j),
368 std::weak_ptr<Content> (_content.front ()),
369 std::weak_ptr<Content> (i)
373 JobManager::instance()->add (j);
378 ContentMenu::re_examine ()
380 auto film = _film.lock ();
385 for (auto i: _content) {
386 JobManager::instance()->add(make_shared<ExamineContentJob>(film, i));
391 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
393 auto job = j.lock ();
394 if (!job || !job->finished_ok ()) {
398 auto old_content = oc.lock ();
399 auto new_content = nc.lock ();
400 DCPOMATIC_ASSERT (old_content);
401 DCPOMATIC_ASSERT (new_content);
403 if (new_content->digest() != old_content->digest()) {
404 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."));
408 old_content->set_paths (new_content->paths());
414 DCPOMATIC_ASSERT (!_content.empty ());
415 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
416 DCPOMATIC_ASSERT (dcp);
418 auto d = new wxFileDialog (_parent, _("Select KDM"));
420 if (d->ShowModal() == wxID_OK) {
421 optional<dcp::EncryptedKDM> kdm;
423 kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
424 } catch (exception& e) {
425 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
430 /* Try to decrypt it to get an early preview of any errors */
432 decrypt_kdm_with_helpful_error (*kdm);
433 } catch (KDMError& e) {
434 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
436 } catch (exception& e) {
437 error_dialog (_parent, e.what());
441 DCPExaminer ex (dcp, true);
443 bool kdm_matches_any_cpl = false;
444 for (auto i: ex.cpls()) {
445 if (i->id() == kdm->cpl_id()) {
446 kdm_matches_any_cpl = true;
450 bool kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
452 if (!kdm_matches_any_cpl) {
453 error_dialog (_parent, _("This KDM was not made for this DCP. You will need a different one."));
457 if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
458 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."));
463 auto film = _film.lock ();
464 DCPOMATIC_ASSERT (film);
465 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
474 DCPOMATIC_ASSERT (!_content.empty ());
475 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
476 DCPOMATIC_ASSERT (dcp);
478 auto d = new wxDirDialog (_parent, _("Select OV"));
480 if (d->ShowModal() == wxID_OK) {
481 dcp->add_ov (wx_to_std (d->GetPath()));
482 shared_ptr<Film> film = _film.lock ();
483 DCPOMATIC_ASSERT (film);
484 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
491 ContentMenu::properties ()
493 auto film = _film.lock ();
494 DCPOMATIC_ASSERT (film);
495 auto d = new ContentPropertiesDialog (_parent, film, _content.front());
502 ContentMenu::advanced ()
504 auto d = new ContentAdvancedDialog (_parent, _content.front());
511 ContentMenu::cpl_selected (wxCommandEvent& ev)
517 DCPOMATIC_ASSERT (!_content.empty ());
518 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
519 DCPOMATIC_ASSERT (dcp);
521 DCPExaminer ex (dcp, true);
522 auto cpls = ex.cpls ();
523 DCPOMATIC_ASSERT (ev.GetId() > 0);
524 DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
526 auto i = cpls.begin ();
527 for (int j = 0; j < ev.GetId() - 1; ++j) {
531 dcp->set_cpl ((*i)->id ());
532 auto film = _film.lock ();
533 DCPOMATIC_ASSERT (film);
534 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));