Catch failures to read missing DCPs in various places.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-2016 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 "lib/playlist.h"
28 #include "lib/film.h"
29 #include "lib/image_content.h"
30 #include "lib/content_factory.h"
31 #include "lib/examine_content_job.h"
32 #include "lib/job_manager.h"
33 #include "lib/exceptions.h"
34 #include "lib/dcp_content.h"
35 #include "lib/dcp_examiner.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/audio_content.h"
38 #include <dcp/cpl.h>
39 #include <dcp/exceptions.h>
40 #include <wx/wx.h>
41 #include <wx/dirdlg.h>
42 #include <boost/foreach.hpp>
43 #include <iostream>
44
45 using std::cout;
46 using std::vector;
47 using std::exception;
48 using std::list;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
52
53 enum {
54         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
55         ID_repeat = 256,
56         ID_join,
57         ID_find_missing,
58         ID_properties,
59         ID_re_examine,
60         ID_kdm,
61         ID_ov,
62         ID_choose_cpl,
63         ID_remove
64 };
65
66 ContentMenu::ContentMenu (wxWindow* p)
67         : _menu (new wxMenu)
68         , _parent (p)
69 {
70         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
71         _join = _menu->Append (ID_join, _("Join"));
72         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
73         _properties = _menu->Append (ID_properties, _("Properties..."));
74         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
75         _menu->AppendSeparator ();
76         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
77         _ov = _menu->Append (ID_ov, _("Add OV..."));
78         _cpl_menu = new wxMenu ();
79         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
80         _menu->AppendSeparator ();
81         _remove = _menu->Append (ID_remove, _("Remove"));
82
83         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::repeat, this), ID_repeat);
84         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::join, this), ID_join);
85         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
86         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::properties, this), ID_properties);
87         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
88         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::kdm, this), ID_kdm);
89         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::ov, this), ID_ov);
90         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::remove, this), ID_remove);
91
92         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
93 }
94
95 ContentMenu::~ContentMenu ()
96 {
97         delete _menu;
98
99         BOOST_FOREACH (boost::signals2::connection& i, _job_connections) {
100                 i.disconnect ();
101         }
102 }
103
104 void
105 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
106 {
107         _film = film;
108         _content = c;
109         _views = v;
110
111         int const N = _cpl_menu->GetMenuItemCount();
112         for (int i = 1; i <= N; ++i) {
113                 _cpl_menu->Delete (i);
114         }
115
116         _repeat->Enable (!_content.empty ());
117
118         int n = 0;
119         BOOST_FOREACH (shared_ptr<Content> i, _content) {
120                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
121                         ++n;
122                 }
123         }
124
125         _join->Enable (n > 1);
126
127         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
128         _properties->Enable (_content.size() == 1);
129         _re_examine->Enable (!_content.empty ());
130
131         if (_content.size() == 1) {
132                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
133                 if (dcp) {
134                         _kdm->Enable (dcp->encrypted ());
135                         _ov->Enable (dcp->needs_assets ());
136                         try {
137                                 DCPExaminer ex (dcp);
138                                 list<shared_ptr<dcp::CPL> > cpls = ex.cpls ();
139                                 _choose_cpl->Enable (cpls.size() > 1);
140                                 /* We can't have 0 as a menu item ID on OS X */
141                                 int id = 1;
142                                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls) {
143                                         wxMenuItem* item = _cpl_menu->AppendCheckItem (
144                                                 id++,
145                                                 wxString::Format (
146                                                         "%s (%s)",
147                                                         std_to_wx(i->annotation_text()).data(),
148                                                         std_to_wx(i->id()).data()
149                                                         )
150                                                 );
151                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
152                                 }
153                         } catch (dcp::DCPReadError) {
154                                 /* The DCP is probably missing */
155                         }
156                 } else {
157                         _kdm->Enable (false);
158                         _ov->Enable (false);
159                         _choose_cpl->Enable (false);
160                 }
161         } else {
162                 _kdm->Enable (false);
163         }
164
165         _remove->Enable (!_content.empty ());
166
167         _parent->PopupMenu (_menu, p);
168 }
169
170 void
171 ContentMenu::repeat ()
172 {
173         if (_content.empty ()) {
174                 return;
175         }
176
177         RepeatDialog* d = new RepeatDialog (_parent);
178         if (d->ShowModal() != wxID_OK) {
179                 d->Destroy ();
180                 return;
181         }
182
183         shared_ptr<Film> film = _film.lock ();
184         if (!film) {
185                 return;
186         }
187
188         film->repeat_content (_content, d->number ());
189         d->Destroy ();
190
191         _content.clear ();
192         _views.clear ();
193 }
194
195 void
196 ContentMenu::join ()
197 {
198         vector<shared_ptr<Content> > fc;
199         BOOST_FOREACH (shared_ptr<Content> i, _content) {
200                 shared_ptr<FFmpegContent> f = dynamic_pointer_cast<FFmpegContent> (i);
201                 if (f) {
202                         fc.push_back (f);
203                 }
204         }
205
206         DCPOMATIC_ASSERT (fc.size() > 1);
207
208         shared_ptr<Film> film = _film.lock ();
209         if (!film) {
210                 return;
211         }
212
213         try {
214                 shared_ptr<FFmpegContent> joined (new FFmpegContent (film, fc));
215                 film->remove_content (_content);
216                 film->examine_and_add_content (joined);
217         } catch (JoinError& e) {
218                 error_dialog (_parent, std_to_wx (e.what ()));
219         }
220 }
221
222 void
223 ContentMenu::remove ()
224 {
225         if (_content.empty ()) {
226                 return;
227         }
228
229         shared_ptr<Film> film = _film.lock ();
230         if (!film) {
231                 return;
232         }
233
234         /* We are removing from the timeline if _views is not empty */
235         bool handled = false;
236         if (!_views.empty ()) {
237                 /* Special case: we only remove FFmpegContent if its video view is selected;
238                    if not, and its audio view is selected, we unmap the audio.
239                 */
240                 BOOST_FOREACH (shared_ptr<Content> i, _content) {
241                         shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i);
242                         if (!fc) {
243                                 continue;
244                         }
245
246                         shared_ptr<TimelineVideoContentView> video;
247                         shared_ptr<TimelineAudioContentView> audio;
248
249                         BOOST_FOREACH (shared_ptr<TimelineContentView> j, _views) {
250                                 shared_ptr<TimelineVideoContentView> v = dynamic_pointer_cast<TimelineVideoContentView> (j);
251                                 shared_ptr<TimelineAudioContentView> a = dynamic_pointer_cast<TimelineAudioContentView> (j);
252                                 if (v && v->content() == fc) {
253                                         video = v;
254                                 } else if (a && a->content() == fc) {
255                                         audio = a;
256                                 }
257                         }
258
259                         if (!video && audio) {
260                                 AudioMapping m = fc->audio->mapping ();
261                                 m.unmap_all ();
262                                 fc->audio->set_mapping (m);
263                                 handled = true;
264                         }
265                 }
266         }
267
268         if (!handled) {
269                 film->remove_content (_content);
270         }
271
272         _content.clear ();
273         _views.clear ();
274 }
275
276 void
277 ContentMenu::find_missing ()
278 {
279         if (_content.size() != 1) {
280                 return;
281         }
282
283         shared_ptr<const Film> film = _film.lock ();
284         if (!film) {
285                 return;
286         }
287
288         shared_ptr<Content> content;
289
290         /* XXX: a bit nasty */
291         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (_content.front ());
292         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (_content.front ());
293
294         int r = wxID_CANCEL;
295         boost::filesystem::path path;
296
297         if ((ic && !ic->still ()) || dc) {
298                 wxDirDialog* d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
299                 r = d->ShowModal ();
300                 path = wx_to_std (d->GetPath ());
301                 d->Destroy ();
302         } else {
303                 wxFileDialog* d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
304                 r = d->ShowModal ();
305                 path = wx_to_std (d->GetPath ());
306                 d->Destroy ();
307         }
308
309         if (r == wxID_OK) {
310                 content = content_factory (film, path);
311         }
312
313         if (!content) {
314                 return;
315         }
316
317         shared_ptr<Job> j (new ExamineContentJob (film, content));
318
319         _job_connections.push_back (
320                 j->Finished.connect (
321                         bind (
322                                 &ContentMenu::maybe_found_missing,
323                                 this,
324                                 boost::weak_ptr<Job> (j),
325                                 boost::weak_ptr<Content> (_content.front ()),
326                                 boost::weak_ptr<Content> (content)
327                                 )
328                         )
329                 );
330
331         JobManager::instance()->add (j);
332 }
333
334 void
335 ContentMenu::re_examine ()
336 {
337         shared_ptr<Film> film = _film.lock ();
338         if (!film) {
339                 return;
340         }
341
342         BOOST_FOREACH (shared_ptr<Content> i, _content) {
343                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, i)));
344         }
345 }
346
347 void
348 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
349 {
350         shared_ptr<Job> job = j.lock ();
351         if (!job || !job->finished_ok ()) {
352                 return;
353         }
354
355         shared_ptr<Content> old_content = oc.lock ();
356         shared_ptr<Content> new_content = nc.lock ();
357         DCPOMATIC_ASSERT (old_content);
358         DCPOMATIC_ASSERT (new_content);
359
360         if (new_content->digest() != old_content->digest()) {
361                 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."));
362                 return;
363         }
364
365         old_content->set_path (new_content->path (0));
366 }
367
368 void
369 ContentMenu::kdm ()
370 {
371         DCPOMATIC_ASSERT (!_content.empty ());
372         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
373         DCPOMATIC_ASSERT (dcp);
374
375         wxFileDialog* d = new wxFileDialog (_parent, _("Select KDM"));
376
377         if (d->ShowModal() == wxID_OK) {
378                 try {
379                         dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()))));
380                 } catch (exception& e) {
381                         error_dialog (_parent, wxString::Format (_("Could not load KDM (%s)"), e.what ()));
382                         d->Destroy ();
383                         return;
384                 }
385
386                 shared_ptr<Film> film = _film.lock ();
387                 DCPOMATIC_ASSERT (film);
388                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
389         }
390
391         d->Destroy ();
392 }
393
394 void
395 ContentMenu::ov ()
396 {
397         DCPOMATIC_ASSERT (!_content.empty ());
398         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
399         DCPOMATIC_ASSERT (dcp);
400
401         wxDirDialog* d = new wxDirDialog (_parent, _("Select OV"));
402
403         if (d->ShowModal() == wxID_OK) {
404                 dcp->add_ov (wx_to_std (d->GetPath ()));
405                 shared_ptr<Film> film = _film.lock ();
406                 DCPOMATIC_ASSERT (film);
407                 JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
408         }
409
410         d->Destroy ();
411 }
412
413 void
414 ContentMenu::properties ()
415 {
416         ContentPropertiesDialog* d = new ContentPropertiesDialog (_parent, _content.front ());
417         d->ShowModal ();
418         d->Destroy ();
419 }
420
421 void
422 ContentMenu::cpl_selected (wxCommandEvent& ev)
423 {
424         DCPOMATIC_ASSERT (!_content.empty ());
425         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (_content.front ());
426         DCPOMATIC_ASSERT (dcp);
427
428         DCPExaminer ex (dcp);
429         list<shared_ptr<dcp::CPL> > cpls = ex.cpls ();
430         DCPOMATIC_ASSERT (ev.GetId() > 0);
431         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
432
433         list<shared_ptr<dcp::CPL> >::const_iterator i = cpls.begin ();
434         for (int j = 0; j < ev.GetId() - 1; ++j) {
435                 ++i;
436         }
437
438         dcp->set_cpl ((*i)->id ());
439         shared_ptr<Film> film = _film.lock ();
440         DCPOMATIC_ASSERT (film);
441         JobManager::instance()->add (shared_ptr<Job> (new ExamineContentJob (film, dcp)));
442 }