BOOST_FOREACH.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-2020 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 #include "content_menu.h"
22 #include "repeat_dialog.h"
23 #include "wx_util.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"
29 #include "lib/film.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"
41 #include <dcp/cpl.h>
42 #include <dcp/exceptions.h>
43 #include <dcp/decrypted_kdm.h>
44 #include <wx/wx.h>
45 #include <wx/dirdlg.h>
46 #include <iostream>
47
48 using std::cout;
49 using std::vector;
50 using std::exception;
51 using std::list;
52 using std::shared_ptr;
53 using std::weak_ptr;
54 using std::dynamic_pointer_cast;
55 using boost::optional;
56 #if BOOST_VERSION >= 106100
57 using namespace boost::placeholders;
58 #endif
59
60
61 enum {
62         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
63         ID_repeat = 256,
64         ID_join,
65         ID_find_missing,
66         ID_properties,
67         ID_advanced,
68         ID_re_examine,
69         ID_kdm,
70         ID_ov,
71         ID_choose_cpl,
72         ID_set_dcp_settings,
73         ID_remove
74 };
75
76 ContentMenu::ContentMenu (wxWindow* p)
77         : _menu (new wxMenu)
78         , _parent (p)
79         , _pop_up_open (false)
80 {
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"));
95
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);
107 }
108
109 void
110 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
111 {
112         _film = film;
113         _content = c;
114         _views = v;
115
116         int const N = _cpl_menu->GetMenuItemCount();
117         for (int i = 1; i <= N; ++i) {
118                 _cpl_menu->Delete (i);
119         }
120
121         _repeat->Enable (!_content.empty ());
122
123         int n = 0;
124         for (auto i: _content) {
125                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
126                         ++n;
127                 }
128         }
129
130         _join->Enable (n > 1);
131
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 ());
136
137         if (_content.size() == 1) {
138                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
139                 if (dcp) {
140                         _kdm->Enable (dcp->encrypted ());
141                         _ov->Enable (dcp->needs_assets ());
142                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
143                         try {
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 */
148                                 int id = 1;
149                                 for (auto i: cpls) {
150                                         wxMenuItem* item = _cpl_menu->AppendRadioItem (
151                                                 id++,
152                                                 wxString::Format (
153                                                         "%s (%s)",
154                                                         std_to_wx(i->annotation_text()).data(),
155                                                         std_to_wx(i->id()).data()
156                                                         )
157                                                 );
158                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
159                                 }
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 */
166                         }
167                 } else {
168                         _kdm->Enable (false);
169                         _ov->Enable (false);
170                         _choose_cpl->Enable (false);
171                         _set_dcp_settings->Enable (false);
172                 }
173         } else {
174                 _kdm->Enable (false);
175                 _set_dcp_settings->Enable (false);
176         }
177
178         _remove->Enable (!_content.empty ());
179
180         _pop_up_open = true;
181         _parent->PopupMenu (_menu, p);
182         _pop_up_open = false;
183 }
184
185
186 void
187 ContentMenu::set_dcp_settings ()
188 {
189         shared_ptr<Film> film = _film.lock ();
190         if (!film) {
191                 return;
192         }
193
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);
198 }
199
200
201 void
202 ContentMenu::repeat ()
203 {
204         if (_content.empty ()) {
205                 return;
206         }
207
208         RepeatDialog* d = new RepeatDialog (_parent);
209         if (d->ShowModal() != wxID_OK) {
210                 d->Destroy ();
211                 return;
212         }
213
214         shared_ptr<Film> film = _film.lock ();
215         if (!film) {
216                 return;
217         }
218
219         film->repeat_content (_content, d->number ());
220         d->Destroy ();
221
222         _content.clear ();
223         _views.clear ();
224 }
225
226 void
227 ContentMenu::join ()
228 {
229         vector<shared_ptr<Content> > fc;
230         for (auto i: _content) {
231                 shared_ptr<FFmpegContent> f = dynamic_pointer_cast<FFmpegContent> (i);
232                 if (f) {
233                         fc.push_back (f);
234                 }
235         }
236
237         DCPOMATIC_ASSERT (fc.size() > 1);
238
239         shared_ptr<Film> film = _film.lock ();
240         if (!film) {
241                 return;
242         }
243
244         try {
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 ()));
250         }
251 }
252
253 void
254 ContentMenu::remove ()
255 {
256         if (_content.empty ()) {
257                 return;
258         }
259
260         shared_ptr<Film> film = _film.lock ();
261         if (!film) {
262                 return;
263         }
264
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.
270                 */
271                 for (auto i: _content) {
272                         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i);
273                         if (!fc) {
274                                 continue;
275                         }
276
277                         shared_ptr<TimelineVideoContentView> video;
278                         shared_ptr<TimelineAudioContentView> audio;
279
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) {
284                                         video = v;
285                                 } else if (a && a->content() == fc) {
286                                         audio = a;
287                                 }
288                         }
289
290                         if (!video && audio) {
291                                 AudioMapping m = fc->audio->mapping ();
292                                 m.unmap_all ();
293                                 fc->audio->set_mapping (m);
294                                 handled = true;
295                         }
296                 }
297         }
298
299         if (!handled) {
300                 film->remove_content (_content);
301         }
302
303         _content.clear ();
304         _views.clear ();
305 }
306
307 void
308 ContentMenu::find_missing ()
309 {
310         if (_content.size() != 1) {
311                 return;
312         }
313
314         shared_ptr<const Film> film = _film.lock ();
315         if (!film) {
316                 return;
317         }
318
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 ());
322
323         int r = wxID_CANCEL;
324         boost::filesystem::path path;
325
326         if ((ic && !ic->still ()) || dc) {
327                 wxDirDialog* d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
328                 r = d->ShowModal ();
329                 path = wx_to_std (d->GetPath ());
330                 d->Destroy ();
331         } else {
332                 wxFileDialog* d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
333                 r = d->ShowModal ();
334                 path = wx_to_std (d->GetPath ());
335                 d->Destroy ();
336         }
337
338         list<shared_ptr<Content> > content;
339
340         if (r == wxID_OK) {
341                 if (dc) {
342                         content.push_back (shared_ptr<DCPContent>(new DCPContent(path)));
343                 } else {
344                         content = content_factory (path);
345                 }
346         }
347
348         if (content.empty ()) {
349                 return;
350         }
351
352         for (auto i: content) {
353                 shared_ptr<Job> j (new ExamineContentJob (film, i));
354
355                 j->Finished.connect (
356                         bind (
357                                 &ContentMenu::maybe_found_missing,
358                                 this,
359                                 std::weak_ptr<Job> (j),
360                                 std::weak_ptr<Content> (_content.front ()),
361                                 std::weak_ptr<Content> (i)
362                                 )
363                         );
364
365                 JobManager::instance()->add (j);
366         }
367 }
368
369 void
370 ContentMenu::re_examine ()
371 {
372         shared_ptr<Film> film = _film.lock ();
373         if (!film) {
374                 return;
375         }
376
377         for (auto i: _content) {
378                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
379         }
380 }
381
382 void
383 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
384 {
385         shared_ptr<Job> job = j.lock ();
386         if (!job || !job->finished_ok ()) {
387                 return;
388         }
389
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);
394
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."));
397                 return;
398         }
399
400         old_content->set_paths (new_content->paths());
401 }
402
403 void
404 ContentMenu::kdm ()
405 {
406         DCPOMATIC_ASSERT (!_content.empty ());
407         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
408         DCPOMATIC_ASSERT (dcp);
409
410         wxFileDialog* d = new wxFileDialog (_parent, _("Select KDM"));
411
412         if (d->ShowModal() == wxID_OK) {
413                 optional<dcp::EncryptedKDM> kdm;
414                 try {
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()));
418                         d->Destroy ();
419                         return;
420                 }
421
422                 /* Try to decrypt it to get an early preview of any errors */
423                 try {
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()));
427                         return;
428                 } catch (exception& e) {
429                         error_dialog (_parent, e.what());
430                         return;
431                 }
432
433                 DCPExaminer ex (dcp, true);
434
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;
439                         }
440                 }
441
442                 bool kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
443
444                 if (!kdm_matches_any_cpl) {
445                         error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
446                         return;
447                 }
448
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."));
451                 }
452
453                 dcp->add_kdm (*kdm);
454
455                 shared_ptr<Film> film = _film.lock ();
456                 DCPOMATIC_ASSERT (film);
457                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
458         }
459
460         d->Destroy ();
461 }
462
463 void
464 ContentMenu::ov ()
465 {
466         DCPOMATIC_ASSERT (!_content.empty ());
467         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
468         DCPOMATIC_ASSERT (dcp);
469
470         wxDirDialog* d = new wxDirDialog (_parent, _("Select OV"));
471
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)));
477         }
478
479         d->Destroy ();
480 }
481
482 void
483 ContentMenu::properties ()
484 {
485         shared_ptr<Film> film = _film.lock ();
486         DCPOMATIC_ASSERT (film);
487         ContentPropertiesDialog* d = new ContentPropertiesDialog (_parent, film, _content.front());
488         d->ShowModal ();
489         d->Destroy ();
490 }
491
492
493 void
494 ContentMenu::advanced ()
495 {
496         ContentAdvancedDialog* d = new ContentAdvancedDialog (_parent, _content.front());
497         d->ShowModal ();
498         d->Destroy ();
499 }
500
501
502 void
503 ContentMenu::cpl_selected (wxCommandEvent& ev)
504 {
505         if (!_pop_up_open) {
506                 return;
507         }
508
509         DCPOMATIC_ASSERT (!_content.empty ());
510         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
511         DCPOMATIC_ASSERT (dcp);
512
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()));
517
518         list<shared_ptr<dcp::CPL> >::const_iterator i = cpls.begin ();
519         for (int j = 0; j < ev.GetId() - 1; ++j) {
520                 ++i;
521         }
522
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)));
527 }