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