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