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