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/examine_content_job.h"
35 #include "lib/exceptions.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/film.h"
38 #include "lib/find_missing.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 <wx/dirdlg.h>
47 #include <wx/wx.h>
48 #include <algorithm>
49 #include <iostream>
50
51
52 using std::cout;
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 (nullptr, _("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 (nullptr, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
341                 r = d->ShowModal ();
342                 path = wx_to_std (d->GetPath());
343                 d->Destroy ();
344         }
345
346         if (r == wxID_CANCEL) {
347                 return;
348         }
349
350         dcpomatic::find_missing (film->content(), path);
351 }
352
353 void
354 ContentMenu::re_examine ()
355 {
356         auto film = _film.lock ();
357         if (!film) {
358                 return;
359         }
360
361         for (auto i: _content) {
362                 JobManager::instance()->add(make_shared<ExamineContentJob>(film, i));
363         }
364 }
365
366
367 void
368 ContentMenu::kdm ()
369 {
370         DCPOMATIC_ASSERT (!_content.empty ());
371         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
372         DCPOMATIC_ASSERT (dcp);
373
374         auto d = new wxFileDialog (_parent, _("Select KDM"));
375
376         if (d->ShowModal() == wxID_OK) {
377                 optional<dcp::EncryptedKDM> kdm;
378                 try {
379                         kdm = dcp::EncryptedKDM (dcp::file_to_string(wx_to_std(d->GetPath()), MAX_KDM_SIZE));
380                 } catch (exception& e) {
381                         error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
382                         d->Destroy ();
383                         return;
384                 }
385
386                 /* Try to decrypt it to get an early preview of any errors */
387                 try {
388                         decrypt_kdm_with_helpful_error (*kdm);
389                 } catch (KDMError& e) {
390                         error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
391                         return;
392                 } catch (exception& e) {
393                         error_dialog (_parent, e.what());
394                         return;
395                 }
396
397                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
398                 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(); });
399                 bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
400
401                 if (!kdm_matches_any_cpl) {
402                         error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
403                         return;
404                 }
405
406                 if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
407                         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."));
408                 }
409
410                 dcp->add_kdm (*kdm);
411
412                 auto film = _film.lock ();
413                 DCPOMATIC_ASSERT (film);
414                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
415         }
416
417         d->Destroy ();
418 }
419
420 void
421 ContentMenu::ov ()
422 {
423         DCPOMATIC_ASSERT (!_content.empty ());
424         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
425         DCPOMATIC_ASSERT (dcp);
426
427         auto d = new wxDirDialog (_parent, _("Select OV"));
428
429         if (d->ShowModal() == wxID_OK) {
430                 dcp->add_ov (wx_to_std (d->GetPath()));
431                 shared_ptr<Film> film = _film.lock ();
432                 DCPOMATIC_ASSERT (film);
433                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
434         }
435
436         d->Destroy ();
437 }
438
439 void
440 ContentMenu::properties ()
441 {
442         auto film = _film.lock ();
443         DCPOMATIC_ASSERT (film);
444         auto d = new ContentPropertiesDialog (_parent, film, _content.front());
445         d->ShowModal ();
446         d->Destroy ();
447 }
448
449
450 void
451 ContentMenu::advanced ()
452 {
453         auto d = new ContentAdvancedDialog (_parent, _content.front());
454         d->ShowModal ();
455         d->Destroy ();
456 }
457
458
459 void
460 ContentMenu::cpl_selected (wxCommandEvent& ev)
461 {
462         if (!_pop_up_open) {
463                 return;
464         }
465
466         DCPOMATIC_ASSERT (!_content.empty ());
467         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
468         DCPOMATIC_ASSERT (dcp);
469
470         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
471         DCPOMATIC_ASSERT (ev.GetId() > 0);
472         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
473
474         auto i = cpls.begin ();
475         for (int j = 0; j < ev.GetId() - 1; ++j) {
476                 ++i;
477         }
478
479         dcp->set_cpl ((*i)->id ());
480         auto film = _film.lock ();
481         DCPOMATIC_ASSERT (film);
482         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
483 }