C++11 tidying.
[dcpomatic.git] / src / wx / playlist_controls.cc
1 /*
2     Copyright (C) 2018-2022 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_view.h"
23 #include "dcpomatic_button.h"
24 #include "film_viewer.h"
25 #include "playlist_controls.h"
26 #include "static_text.h"
27 #include "wx_util.h"
28 #include "lib/compose.hpp"
29 #include "lib/cross.h"
30 #include "lib/dcp_content.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/internet.h"
33 #include "lib/player_video.h"
34 #include "lib/scoped_temporary.h"
35 #include <dcp/exceptions.h>
36 #include <dcp/raw_convert.h>
37 #include <wx/listctrl.h>
38 #include <wx/progdlg.h>
39
40
41 using std::cout;
42 using std::dynamic_pointer_cast;
43 using std::exception;
44 using std::make_shared;
45 using std::shared_ptr;
46 using std::sort;
47 using std::string;
48 using boost::optional;
49 using namespace dcpomatic;
50
51
52 PlaylistControls::PlaylistControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
53         : Controls (parent, viewer, false)
54         , _play_button (new Button(this, _("Play")))
55         , _pause_button (new Button(this, _("Pause")))
56         , _stop_button (new Button(this, _("Stop")))
57         , _next_button (new Button(this, "Next"))
58         , _previous_button (new Button(this, "Previous"))
59 {
60         _button_sizer->Add (_previous_button, 0, wxEXPAND);
61         _button_sizer->Add (_play_button, 0, wxEXPAND);
62         _button_sizer->Add (_pause_button, 0, wxEXPAND);
63         _button_sizer->Add (_stop_button, 0, wxEXPAND);
64         _button_sizer->Add (_next_button, 0, wxEXPAND);
65
66         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
67         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
68
69         auto left_sizer = new wxBoxSizer (wxVERTICAL);
70         auto e_sizer = new wxBoxSizer (wxHORIZONTAL);
71
72         wxFont subheading_font (*wxNORMAL_FONT);
73         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
74
75         auto spl_header = new wxBoxSizer (wxHORIZONTAL);
76         {
77                 wxStaticText* m = new StaticText (this, "Playlists");
78                 m->SetFont (subheading_font);
79                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
80         }
81         _refresh_spl_view = new Button (this, "Refresh");
82         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
83
84         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
85         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
86
87         _content_view = new ContentView (this);
88
89         auto content_header = new wxBoxSizer (wxHORIZONTAL);
90         {
91                 wxStaticText* m = new StaticText (this, "Content");
92                 m->SetFont (subheading_font);
93                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
94         }
95         _refresh_content_view = new Button (this, "Refresh");
96         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
97
98         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
99         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
100
101         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
102         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
103         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
104         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
105         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
106
107         _v_sizer->Add (e_sizer, 1, wxEXPAND);
108
109         _play_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::play_clicked,  this));
110         _pause_button->Bind    (wxEVT_BUTTON, boost::bind(&PlaylistControls::pause_clicked, this));
111         _stop_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::stop_clicked,  this));
112         _next_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::next_clicked,  this));
113         _previous_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::previous_clicked,  this));
114         _spl_view->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&PlaylistControls::spl_selection_changed, this));
115         _spl_view->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
116         viewer->Finished.connect (boost::bind(&PlaylistControls::viewer_finished, this));
117         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::update_playlist_directory, this));
118         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
119
120         _content_view->update ();
121         update_playlist_directory ();
122 }
123
124 void
125 PlaylistControls::started ()
126 {
127         Controls::started ();
128         _play_button->Enable (false);
129         _pause_button->Enable (true);
130 }
131
132 /** Called when the viewer finishes a single piece of content, or it is explicitly stopped */
133 void
134 PlaylistControls::stopped ()
135 {
136         Controls::stopped ();
137         _play_button->Enable (true);
138         _pause_button->Enable (false);
139 }
140
141 void
142 PlaylistControls::deselect_playlist ()
143 {
144         long int const selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
145         if (selected != -1) {
146                 _selected_playlist = boost::none;
147                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
148         }
149         ResetFilm (std::make_shared<Film>(optional<boost::filesystem::path>()));
150 }
151
152 void
153 PlaylistControls::play_clicked ()
154 {
155         auto viewer = _viewer.lock ();
156         if (viewer) {
157                 viewer->start ();
158         }
159 }
160
161 void
162 PlaylistControls::setup_sensitivity ()
163 {
164         auto viewer = _viewer.lock ();
165         if (!viewer) {
166                 return;
167         }
168
169         Controls::setup_sensitivity ();
170         bool const active_job = _active_job && *_active_job != "examine_content";
171         bool const c = _film && !_film->content().empty() && !active_job;
172         _play_button->Enable (c && !viewer->playing());
173         _pause_button->Enable (viewer->playing());
174         _spl_view->Enable (!viewer->playing());
175         _next_button->Enable (can_do_next());
176         _previous_button->Enable (can_do_previous());
177 }
178
179 void
180 PlaylistControls::pause_clicked ()
181 {
182         auto viewer = _viewer.lock ();
183         if (viewer) {
184                 viewer->stop ();
185         }
186 }
187
188 void
189 PlaylistControls::stop_clicked ()
190 {
191         auto viewer = _viewer.lock ();
192         if (!viewer) {
193                 return;
194         }
195
196         viewer->stop ();
197         viewer->seek (DCPTime(), true);
198         if (_selected_playlist) {
199                 _selected_playlist_position = 0;
200                 update_current_content ();
201         }
202         deselect_playlist ();
203 }
204
205 bool
206 PlaylistControls::can_do_previous ()
207 {
208         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
209 }
210
211 void
212 PlaylistControls::previous_clicked ()
213 {
214         if (!can_do_previous ()) {
215                 return;
216         }
217
218         _selected_playlist_position--;
219         update_current_content ();
220 }
221
222 bool
223 PlaylistControls::can_do_next ()
224 {
225         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
226 }
227
228 void
229 PlaylistControls::next_clicked ()
230 {
231         if (!can_do_next ()) {
232                 return;
233         }
234
235         _selected_playlist_position++;
236         update_current_content ();
237 }
238
239
240 void
241 PlaylistControls::add_playlist_to_list (SPL spl)
242 {
243         int const N = _spl_view->GetItemCount();
244
245         wxListItem it;
246         it.SetId(N);
247         it.SetColumn(0);
248         string t = spl.name();
249         if (spl.missing()) {
250                 t += " (content missing)";
251         }
252         it.SetText (std_to_wx(t));
253         _spl_view->InsertItem (it);
254 }
255
256 struct SPLComparator
257 {
258         bool operator() (SPL const & a, SPL const & b) {
259                 return a.name() < b.name();
260         }
261 };
262
263 void
264 PlaylistControls::update_playlist_directory ()
265 {
266         using namespace boost::filesystem;
267
268         _spl_view->DeleteAllItems ();
269         optional<path> dir = Config::instance()->player_playlist_directory();
270         if (!dir) {
271                 return;
272         }
273
274         _playlists.clear ();
275
276         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
277                 try {
278                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
279                                 SPL spl;
280                                 spl.read (i->path(), _content_view);
281                                 _playlists.push_back (spl);
282                         }
283                 } catch (exception& e) {
284                         /* Never mind */
285                 }
286         }
287
288         sort (_playlists.begin(), _playlists.end(), SPLComparator());
289         for (auto i: _playlists) {
290                 add_playlist_to_list (i);
291         }
292
293         _selected_playlist = boost::none;
294 }
295
296 optional<dcp::EncryptedKDM>
297 PlaylistControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
298 {
299         using namespace boost::filesystem;
300         auto kdm_dir = Config::instance()->player_kdm_directory();
301         if (!kdm_dir) {
302                 return {};
303         }
304         for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
305                 try {
306                         if (file_size(i->path()) < MAX_KDM_SIZE) {
307                                 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
308                                 if (kdm.cpl_id() == dcp->cpl()) {
309                                         return kdm;
310                                 }
311                         }
312                 } catch (std::exception& e) {
313                         /* Hey well */
314                 }
315         }
316         return optional<dcp::EncryptedKDM>();
317 }
318
319 void
320 PlaylistControls::spl_selection_changed ()
321 {
322         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
323         if (selected == -1) {
324                 _current_spl_view->DeleteAllItems ();
325                 _selected_playlist = boost::none;
326                 return;
327         }
328
329         if (_playlists[selected].missing()) {
330                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
331                 deselect_playlist ();
332                 return;
333         }
334
335         if (_playlists[selected].get().empty()) {
336                 error_dialog (this, "This playlist is empty.");
337                 return;
338         }
339
340         select_playlist (selected, 0);
341 }
342
343 void
344 PlaylistControls::select_playlist (int selected, int position)
345 {
346         wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
347
348         for (auto const& i: _playlists[selected].get()) {
349                 dialog.Pulse ();
350                 auto dcp = dynamic_pointer_cast<DCPContent> (i.content);
351                 if (dcp && dcp->needs_kdm()) {
352                         optional<dcp::EncryptedKDM> kdm;
353                         kdm = get_kdm_from_directory (dcp);
354                         if (kdm) {
355                                 try {
356                                         dcp->add_kdm (*kdm);
357                                         dcp->examine (_film, shared_ptr<Job>());
358                                 } catch (KDMError& e) {
359                                         error_dialog (this, "Could not load KDM.");
360                                 }
361                         }
362                         if (dcp->needs_kdm()) {
363                                 /* We didn't get a KDM for this */
364                                 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
365                                 deselect_playlist ();
366                                 return;
367                         }
368                 }
369         }
370
371         _current_spl_view->DeleteAllItems ();
372
373         int N = 0;
374         for (auto i: _playlists[selected].get()) {
375                 wxListItem it;
376                 it.SetId (N);
377                 it.SetColumn (0);
378                 it.SetText (std_to_wx(i.name));
379                 _current_spl_view->InsertItem (it);
380                 ++N;
381         }
382
383         _selected_playlist = selected;
384         _selected_playlist_position = position;
385         dialog.Pulse ();
386         reset_film ();
387         dialog.Pulse ();
388         update_current_content ();
389 }
390
391 void
392 PlaylistControls::reset_film ()
393 {
394         DCPOMATIC_ASSERT (_selected_playlist);
395         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
396         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
397         ResetFilm (film);
398 }
399
400 void
401 PlaylistControls::config_changed (int property)
402 {
403         Controls::config_changed (property);
404
405         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
406                 _content_view->update ();
407         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
408                 update_playlist_directory ();
409         }
410 }
411
412 void
413 PlaylistControls::set_film (shared_ptr<Film> film)
414 {
415         Controls::set_film (film);
416         setup_sensitivity ();
417 }
418
419 void
420 PlaylistControls::update_current_content ()
421 {
422         DCPOMATIC_ASSERT (_selected_playlist);
423
424         wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
425
426         setup_sensitivity ();
427         dialog.Pulse ();
428         reset_film ();
429 }
430
431 /** One piece of content in our SPL has finished playing */
432 void
433 PlaylistControls::viewer_finished ()
434 {
435         auto viewer = _viewer.lock ();
436         if (!_selected_playlist || !viewer) {
437                 return;
438         }
439
440         _selected_playlist_position++;
441         if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
442                 /* Next piece of content on the SPL */
443                 update_current_content ();
444                 viewer->start ();
445         } else {
446                 /* Finished the whole SPL */
447                 _selected_playlist_position = 0;
448                 ResetFilm (std::make_shared<Film>(optional<boost::filesystem::path>()));
449                 _play_button->Enable (true);
450                 _pause_button->Enable (false);
451         }
452 }
453
454 void
455 PlaylistControls::play ()
456 {
457         play_clicked ();
458 }
459
460 void
461 PlaylistControls::stop ()
462 {
463         stop_clicked ();
464 }