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