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