Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[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         cout << "check_restart called\n";
129         FILE* f = fopen_boost (Config::path("position"), "r");
130         if (!f) {
131                 cout << "could not open position file (" << errno << ")\n";
132                 return;
133         }
134
135         char id[64];
136         int index;
137         int64_t time;
138         fscanf (f, "%63s %d %ld", id, &index, &time);
139
140         cout << "Looking for playlist " << id << " to restart.\n";
141
142         for (size_t i = 0; i < _playlists.size(); ++i) {
143                 if (_playlists[i].id() == id) {
144                         cout << "Found playlist " << id << "\n";
145                         select_playlist (i, index);
146                         update_current_content ();
147                         _viewer->seek (DCPTime(time), false);
148                         _viewer->start ();
149                 }
150         }
151
152         fclose (f);
153 }
154
155 void
156 SwaroopControls::viewer_position_changed ()
157 {
158         if (!_selected_playlist || !_viewer->playing() || _viewer->position().get() % DCPTime::HZ) {
159                 return;
160         }
161
162         FILE* f = fopen_boost (Config::path("position"), "w");
163         if (f) {
164                 string const p = _playlists[*_selected_playlist].id()
165                         + " " + dcp::raw_convert<string>(_selected_playlist_position)
166                         + " " + dcp::raw_convert<string>(_viewer->position().get());
167
168                 checked_fwrite (p.c_str(), p.length(), f, Config::path("position"));
169 #ifdef DCPOMATIC_LINUX
170                 fflush (f);
171                 fsync (fileno(f));
172 #endif
173                 fclose (f);
174         }
175 }
176
177 void
178 SwaroopControls::started ()
179 {
180         Controls::started ();
181         _play_button->Enable (false);
182         _pause_button->Enable (true);
183         _viewer->set_background_image (false);
184 }
185
186 void
187 SwaroopControls::stopped ()
188 {
189         Controls::stopped ();
190         _play_button->Enable (true);
191         _pause_button->Enable (false);
192 }
193
194 void
195 SwaroopControls::play_clicked ()
196 {
197         _viewer->start ();
198 }
199
200 void
201 SwaroopControls::setup_sensitivity ()
202 {
203         Controls::setup_sensitivity ();
204         bool const active_job = _active_job && *_active_job != "examine_content";
205         bool const c = _film && !_film->content().empty() && !active_job;
206         _play_button->Enable (c && !_viewer->playing());
207         _pause_button->Enable (_viewer->playing());
208         _slider->Enable (!_current_disable_timeline);
209         _spl_view->Enable (!_viewer->playing());
210         _next_button->Enable (!_current_disable_next && can_do_next());
211         _previous_button->Enable (can_do_previous());
212 }
213
214 void
215 SwaroopControls::pause_clicked ()
216 {
217         _viewer->stop ();
218 }
219
220 void
221 SwaroopControls::stop_clicked ()
222 {
223         _viewer->stop ();
224         _viewer->seek (DCPTime(), true);
225         if (_selected_playlist) {
226                 _selected_playlist_position = 0;
227                 update_current_content ();
228         }
229         _viewer->set_background_image (true);
230 }
231
232 bool
233 SwaroopControls::can_do_previous ()
234 {
235         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
236 }
237
238 void
239 SwaroopControls::previous_clicked ()
240 {
241         if (!can_do_previous ()) {
242                 return;
243         }
244
245         _selected_playlist_position--;
246         update_current_content ();
247 }
248
249 bool
250 SwaroopControls::can_do_next ()
251 {
252         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
253 }
254
255 void
256 SwaroopControls::next_clicked ()
257 {
258         if (!can_do_next ()) {
259                 return;
260         }
261
262         _selected_playlist_position++;
263         update_current_content ();
264 }
265
266 void
267 SwaroopControls::log (wxString s)
268 {
269         struct timeval time;
270         gettimeofday (&time, 0);
271         char buffer[64];
272         time_t const sec = time.tv_sec;
273         struct tm* t = localtime (&sec);
274         strftime (buffer, 64, "%c", t);
275         wxString ts = std_to_wx(string(buffer)) + N_(": ");
276         _log->SetValue(_log->GetValue() + ts + s + "\n");
277
278         optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
279         if (!log) {
280                 return;
281         }
282
283         FILE* f = fopen_boost (*log, "a");
284         fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
285         fclose (f);
286 }
287
288 void
289 SwaroopControls::add_playlist_to_list (SPL spl)
290 {
291         int const N = _spl_view->GetItemCount();
292
293         wxListItem it;
294         it.SetId(N);
295         it.SetColumn(0);
296         string t = spl.name();
297         if (spl.missing()) {
298                 t += " (content missing)";
299         }
300         it.SetText (std_to_wx(t));
301         _spl_view->InsertItem (it);
302 }
303
304 struct SPLComparator
305 {
306         bool operator() (SPL const & a, SPL const & b) {
307                 return a.name() < b.name();
308         }
309 };
310
311 void
312 SwaroopControls::update_playlist_directory ()
313 {
314         using namespace boost::filesystem;
315
316         _spl_view->DeleteAllItems ();
317         optional<path> dir = Config::instance()->player_playlist_directory();
318         if (!dir) {
319                 return;
320         }
321
322         _playlists.clear ();
323
324         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
325                 try {
326                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
327                                 SPL spl;
328                                 spl.read (i->path(), _content_view);
329                                 _playlists.push_back (spl);
330                         }
331                 } catch (exception& e) {
332                         /* Never mind */
333                 }
334         }
335
336         sort (_playlists.begin(), _playlists.end(), SPLComparator());
337         BOOST_FOREACH (SPL i, _playlists) {
338                 add_playlist_to_list (i);
339         }
340
341         _selected_playlist = boost::none;
342 }
343
344 optional<dcp::EncryptedKDM>
345 SwaroopControls::get_kdm_from_url (shared_ptr<DCPContent> dcp)
346 {
347         ScopedTemporary temp;
348         string url = Config::instance()->kdm_server_url();
349         boost::algorithm::replace_all (url, "{CPL}", *dcp->cpl());
350         optional<dcp::EncryptedKDM> kdm;
351         if (dcp->cpl() && !get_from_url(url, false, temp)) {
352                 try {
353                         kdm = dcp::EncryptedKDM (dcp::file_to_string(temp.file()));
354                         if (kdm->cpl_id() != dcp->cpl()) {
355                                 kdm = boost::none;
356                         }
357                 } catch (std::exception& e) {
358                         /* Hey well */
359                 }
360         }
361         return kdm;
362 }
363
364 optional<dcp::EncryptedKDM>
365 SwaroopControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
366 {
367         using namespace boost::filesystem;
368         optional<path> kdm_dir = Config::instance()->player_kdm_directory();
369         if (!kdm_dir) {
370                 return optional<dcp::EncryptedKDM>();
371         }
372         for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
373                 try {
374                         if (file_size(i->path()) < MAX_KDM_SIZE) {
375                                 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
376                                 if (kdm.cpl_id() == dcp->cpl()) {
377                                         return kdm;
378                                 }
379                         }
380                 } catch (std::exception& e) {
381                         /* Hey well */
382                 }
383         }
384         return optional<dcp::EncryptedKDM>();
385 }
386
387 void
388 SwaroopControls::spl_selection_changed ()
389 {
390         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
391         if (selected == -1) {
392                 _current_spl_view->DeleteAllItems ();
393                 _selected_playlist = boost::none;
394                 return;
395         }
396
397         if (_playlists[selected].missing()) {
398                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
399                 _selected_playlist = boost::none;
400                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
401                 return;
402         }
403
404         if (_playlists[selected].get().empty()) {
405                 error_dialog (this, "This playlist is empty.");
406                 return;
407         }
408
409         select_playlist (selected, 0);
410 }
411
412 void
413 SwaroopControls::select_playlist (int selected, int position)
414 {
415         log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
416
417         wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
418
419         BOOST_FOREACH (SPLEntry const & i, _playlists[selected].get()) {
420                 dialog.Pulse ();
421                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
422                 if (dcp && dcp->needs_kdm()) {
423                         optional<dcp::EncryptedKDM> kdm;
424                         kdm = get_kdm_from_url (dcp);
425                         if (!kdm) {
426                                 kdm = get_kdm_from_directory (dcp);
427                         }
428                         if (kdm) {
429                                 try {
430                                         dcp->add_kdm (*kdm);
431                                         dcp->examine (_film, shared_ptr<Job>());
432                                 } catch (KDMError& e) {
433                                         error_dialog (this, "Could not load KDM.");
434                                 }
435                         }
436                         if (dcp->needs_kdm()) {
437                                 /* We didn't get a KDM for this */
438                                 error_dialog (this, "This playlist cannot be loaded as a KDM is missing.");
439                                 _selected_playlist = boost::none;
440                                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
441                                 return;
442                         }
443                 }
444         }
445
446         _current_spl_view->DeleteAllItems ();
447
448         int N = 0;
449         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
450                 wxListItem it;
451                 it.SetId (N);
452                 it.SetColumn (0);
453                 it.SetText (std_to_wx(i.name));
454                 _current_spl_view->InsertItem (it);
455                 ++N;
456         }
457
458         _selected_playlist = selected;
459         _selected_playlist_position = position;
460         dialog.Pulse ();
461         reset_film ();
462         dialog.Pulse ();
463         update_current_content ();
464 }
465
466 void
467 SwaroopControls::reset_film ()
468 {
469         DCPOMATIC_ASSERT (_selected_playlist);
470         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
471         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
472         ResetFilm (film);
473 }
474
475 void
476 SwaroopControls::config_changed (int property)
477 {
478         Controls::config_changed (property);
479
480         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
481                 _content_view->update ();
482         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
483                 update_playlist_directory ();
484         }
485 }
486
487 void
488 SwaroopControls::set_film (shared_ptr<Film> film)
489 {
490         Controls::set_film (film);
491         setup_sensitivity ();
492 }
493
494 void
495 SwaroopControls::update_current_content ()
496 {
497         DCPOMATIC_ASSERT (_selected_playlist);
498
499         wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
500
501         SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
502         _current_disable_timeline = e.disable_timeline;
503         _current_disable_next = !e.skippable;
504
505         setup_sensitivity ();
506         dialog.Pulse ();
507         reset_film ();
508 }
509
510 void
511 SwaroopControls::viewer_finished ()
512 {
513         if (!_selected_playlist) {
514                 return;
515         }
516
517         bool const stop = _playlists[*_selected_playlist].get()[_selected_playlist_position].stop_after_play;
518
519         _selected_playlist_position++;
520         if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
521                 update_current_content ();
522                 if (!stop) {
523                         _viewer->start ();
524                 }
525         } else {
526                 _selected_playlist_position = 0;
527                 _viewer->set_background_image (true);
528                 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
529         }
530 }