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