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_menu.h"
23 #include "repeat_dialog.h"
25 #include "timeline_video_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "content_properties_dialog.h"
28 #include "content_advanced_dialog.h"
29 #include "lib/playlist.h"
31 #include "lib/image_content.h"
32 #include "lib/content_factory.h"
33 #include "lib/examine_content_job.h"
34 #include "lib/job_manager.h"
35 #include "lib/exceptions.h"
36 #include "lib/dcp_content.h"
37 #include "lib/dcp_examiner.h"
38 #include "lib/ffmpeg_content.h"
39 #include "lib/audio_content.h"
40 #include "lib/config.h"
41 #include "lib/copy_dcp_details_to_film.h"
43 #include <dcp/exceptions.h>
44 #include <dcp/decrypted_kdm.h>
45 #include <dcp/search.h>
47 #include <wx/dirdlg.h>
55 using std::shared_ptr;
57 using std::dynamic_pointer_cast;
58 using std::make_shared;
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 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
150 _choose_cpl->Enable (cpls.size() > 1);
151 /* We can't have 0 as a menu item ID on OS X */
154 auto item = _cpl_menu->AppendRadioItem (
158 std_to_wx(i->annotation_text().get_value_or("")).data(),
159 std_to_wx(i->id()).data()
162 item->Check (dcp->cpl() && dcp->cpl() == i->id());
164 } catch (dcp::ReadError &) {
165 /* The DCP is probably missing */
166 } catch (dcp::KDMDecryptionError &) {
167 /* We have an incorrect KDM */
168 } catch (KDMError &) {
169 /* We have an incorrect KDM */
172 _kdm->Enable (false);
174 _choose_cpl->Enable (false);
175 _set_dcp_settings->Enable (false);
178 _kdm->Enable (false);
179 _set_dcp_settings->Enable (false);
182 _remove->Enable (!_content.empty ());
185 _parent->PopupMenu (_menu, p);
186 _pop_up_open = false;
191 ContentMenu::set_dcp_settings ()
193 auto film = _film.lock ();
198 DCPOMATIC_ASSERT (_content.size() == 1);
199 auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
200 DCPOMATIC_ASSERT (dcp);
201 copy_dcp_details_to_film (dcp, film);
206 ContentMenu::repeat ()
208 if (_content.empty ()) {
212 auto d = new RepeatDialog (_parent);
213 if (d->ShowModal() != wxID_OK) {
218 auto film = _film.lock ();
223 film->repeat_content (_content, d->number ());
234 vector<shared_ptr<Content>> fc;
235 for (auto i: _content) {
236 auto f = dynamic_pointer_cast<FFmpegContent> (i);
242 DCPOMATIC_ASSERT (fc.size() > 1);
244 auto film = _film.lock ();
250 auto joined = make_shared<FFmpegContent>(fc);
251 film->remove_content (_content);
252 film->examine_and_add_content (joined);
253 } catch (JoinError& e) {
254 error_dialog (_parent, std_to_wx (e.what ()));
260 ContentMenu::remove ()
262 if (_content.empty ()) {
266 auto film = _film.lock ();
271 /* We are removing from the timeline if _views is not empty */
272 bool handled = false;
273 if (!_views.empty ()) {
274 /* Special case: we only remove FFmpegContent if its video view is selected;
275 if not, and its audio view is selected, we unmap the audio.
277 for (auto i: _content) {
278 auto fc = dynamic_pointer_cast<FFmpegContent> (i);
283 shared_ptr<TimelineVideoContentView> video;
284 shared_ptr<TimelineAudioContentView> audio;
286 for (auto j: _views) {
287 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
288 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
289 if (v && v->content() == fc) {
291 } else if (a && a->content() == fc) {
296 if (!video && audio) {
297 auto m = fc->audio->mapping ();
299 fc->audio->set_mapping (m);
306 film->remove_content (_content);
315 ContentMenu::find_missing ()
317 if (_content.size() != 1) {
321 auto film = _film.lock ();
326 /* XXX: a bit nasty */
327 auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
328 auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
331 boost::filesystem::path path;
333 if ((ic && !ic->still ()) || dc) {
334 auto d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
336 path = wx_to_std (d->GetPath ());
339 auto d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
341 path = wx_to_std (d->GetPath ());
345 list<shared_ptr<Content>> content;
349 content.push_back (make_shared<DCPContent>(path));
351 content = content_factory (path);
355 if (content.empty ()) {
359 for (auto i: content) {
360 auto j = make_shared<ExamineContentJob>(film, i);
362 j->Finished.connect (
364 &ContentMenu::maybe_found_missing,
366 std::weak_ptr<Job> (j),
367 std::weak_ptr<Content> (_content.front ()),
368 std::weak_ptr<Content> (i)
372 JobManager::instance()->add (j);
377 ContentMenu::re_examine ()
379 auto film = _film.lock ();
384 for (auto i: _content) {
385 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
390 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
392 auto job = j.lock ();
393 if (!job || !job->finished_ok ()) {
397 auto old_content = oc.lock ();
398 auto new_content = nc.lock ();
399 DCPOMATIC_ASSERT (old_content);
400 DCPOMATIC_ASSERT (new_content);
402 if (new_content->digest() != old_content->digest()) {
403 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."));
407 old_content->set_paths (new_content->paths());
413 DCPOMATIC_ASSERT (!_content.empty ());
414 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
415 DCPOMATIC_ASSERT (dcp);
417 auto d = new wxFileDialog (_parent, _("Select KDM"));
419 if (d->ShowModal() == wxID_OK) {
420 optional<dcp::EncryptedKDM> kdm;
422 kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
423 } catch (exception& e) {
424 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
429 /* Try to decrypt it to get an early preview of any errors */
431 decrypt_kdm_with_helpful_error (*kdm);
432 } catch (KDMError& e) {
433 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
435 } catch (exception& e) {
436 error_dialog (_parent, e.what());
440 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
441 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(); });
442 bool const 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 auto film = _film.lock ();
456 DCPOMATIC_ASSERT (film);
457 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
466 DCPOMATIC_ASSERT (!_content.empty ());
467 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
468 DCPOMATIC_ASSERT (dcp);
470 auto 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 (make_shared<ExamineContentJob>(film, dcp));
483 ContentMenu::properties ()
485 auto film = _film.lock ();
486 DCPOMATIC_ASSERT (film);
487 auto d = new ContentPropertiesDialog (_parent, film, _content.front());
494 ContentMenu::advanced ()
496 auto d = new ContentAdvancedDialog (_parent, _content.front());
503 ContentMenu::cpl_selected (wxCommandEvent& ev)
509 DCPOMATIC_ASSERT (!_content.empty ());
510 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
511 DCPOMATIC_ASSERT (dcp);
513 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
514 DCPOMATIC_ASSERT (ev.GetId() > 0);
515 DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
517 auto i = cpls.begin ();
518 for (int j = 0; j < ev.GetId() - 1; ++j) {
522 dcp->set_cpl ((*i)->id ());
523 auto film = _film.lock ();
524 DCPOMATIC_ASSERT (film);
525 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));