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/image_content.h"
40 #include "lib/job_manager.h"
41 #include "lib/playlist.h"
43 #include <dcp/decrypted_kdm.h>
44 #include <dcp/exceptions.h>
45 #include <dcp/search.h>
46 #include <wx/dirdlg.h>
50 using std::dynamic_pointer_cast;
53 using std::make_shared;
54 using std::shared_ptr;
57 using boost::optional;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
64 /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
79 ContentMenu::ContentMenu (wxWindow* p)
82 , _pop_up_open (false)
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"));
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);
113 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
119 int const N = _cpl_menu->GetMenuItemCount();
120 for (int i = 1; i <= N; ++i) {
121 _cpl_menu->Delete (i);
124 _repeat->Enable (!_content.empty ());
127 for (auto i: _content) {
128 if (dynamic_pointer_cast<FFmpegContent> (i)) {
133 _join->Enable (n > 1);
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 ());
140 if (_content.size() == 1) {
141 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
143 _kdm->Enable (dcp->encrypted ());
144 _ov->Enable (dcp->needs_assets ());
145 _set_dcp_settings->Enable (static_cast<bool>(dcp));
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 */
152 auto item = _cpl_menu->AppendRadioItem (
156 std_to_wx(i->annotation_text().get_value_or("")).data(),
157 std_to_wx(i->id()).data()
160 item->Check (dcp->cpl() && dcp->cpl() == i->id());
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 */
170 _kdm->Enable (false);
172 _choose_cpl->Enable (false);
173 _set_dcp_settings->Enable (false);
176 _kdm->Enable (false);
177 _set_dcp_settings->Enable (false);
180 _remove->Enable (!_content.empty ());
183 _parent->PopupMenu (_menu, p);
184 _pop_up_open = false;
189 ContentMenu::set_dcp_settings ()
191 auto film = _film.lock ();
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);
204 ContentMenu::repeat ()
206 if (_content.empty ()) {
210 auto d = new RepeatDialog (_parent);
211 if (d->ShowModal() != wxID_OK) {
216 auto film = _film.lock ();
221 film->repeat_content (_content, d->number ());
232 vector<shared_ptr<Content>> fc;
233 for (auto i: _content) {
234 auto f = dynamic_pointer_cast<FFmpegContent> (i);
240 DCPOMATIC_ASSERT (fc.size() > 1);
242 auto film = _film.lock ();
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 ()));
258 ContentMenu::remove ()
260 if (_content.empty ()) {
264 auto film = _film.lock ();
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.
275 for (auto i: _content) {
276 auto fc = dynamic_pointer_cast<FFmpegContent> (i);
281 shared_ptr<TimelineVideoContentView> video;
282 shared_ptr<TimelineAudioContentView> audio;
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) {
289 } else if (a && a->content() == fc) {
294 if (!video && audio) {
295 auto m = fc->audio->mapping ();
297 fc->audio->set_mapping (m);
304 film->remove_content (_content);
313 ContentMenu::find_missing ()
315 if (_content.size() != 1) {
319 auto film = _film.lock ();
324 /* XXX: a bit nasty */
325 auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
326 auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
329 boost::filesystem::path path;
331 if ((ic && !ic->still ()) || dc) {
332 auto d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
334 path = wx_to_std (d->GetPath ());
337 auto d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
339 path = wx_to_std (d->GetPath ());
343 list<shared_ptr<Content>> content;
347 content.push_back (make_shared<DCPContent>(path));
349 content = content_factory (path);
353 if (content.empty ()) {
357 for (auto i: content) {
358 auto j = make_shared<ExamineContentJob>(film, i);
360 j->Finished.connect (
362 &ContentMenu::maybe_found_missing,
364 std::weak_ptr<Job> (j),
365 std::weak_ptr<Content> (_content.front ()),
366 std::weak_ptr<Content> (i)
370 JobManager::instance()->add (j);
375 ContentMenu::re_examine ()
377 auto film = _film.lock ();
382 for (auto i: _content) {
383 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
388 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
390 auto job = j.lock ();
391 if (!job || !job->finished_ok ()) {
395 auto old_content = oc.lock ();
396 auto new_content = nc.lock ();
397 DCPOMATIC_ASSERT (old_content);
398 DCPOMATIC_ASSERT (new_content);
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."));
405 old_content->set_paths (new_content->paths());
411 DCPOMATIC_ASSERT (!_content.empty ());
412 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
413 DCPOMATIC_ASSERT (dcp);
415 auto d = new wxFileDialog (_parent, _("Select KDM"));
417 if (d->ShowModal() == wxID_OK) {
418 optional<dcp::EncryptedKDM> kdm;
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()));
427 /* Try to decrypt it to get an early preview of any errors */
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()));
433 } catch (exception& e) {
434 error_dialog (_parent, e.what());
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();
442 if (!kdm_matches_any_cpl) {
443 error_dialog (_parent, _("This KDM was not made for this DCP. You will need a different one."));
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."));
453 auto film = _film.lock ();
454 DCPOMATIC_ASSERT (film);
455 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
464 DCPOMATIC_ASSERT (!_content.empty ());
465 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
466 DCPOMATIC_ASSERT (dcp);
468 auto d = new wxDirDialog (_parent, _("Select OV"));
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));
481 ContentMenu::properties ()
483 auto film = _film.lock ();
484 DCPOMATIC_ASSERT (film);
485 auto d = new ContentPropertiesDialog (_parent, film, _content.front());
492 ContentMenu::advanced ()
494 auto d = new ContentAdvancedDialog (_parent, _content.front());
501 ContentMenu::cpl_selected (wxCommandEvent& ev)
507 DCPOMATIC_ASSERT (!_content.empty ());
508 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
509 DCPOMATIC_ASSERT (dcp);
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()));
515 auto i = cpls.begin ();
516 for (int j = 0; j < ev.GetId() - 1; ++j) {
520 dcp->set_cpl ((*i)->id ());
521 auto film = _film.lock ();
522 DCPOMATIC_ASSERT (film);
523 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));