Remove C++11-ism.
[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                 }
140         }
141
142         fclose (f);
143 }
144
145 void
146 SwaroopControls::viewer_position_changed ()
147 {
148         if (!_selected_playlist || !_viewer->playing() || _viewer->position().get() % DCPTime::HZ) {
149                 return;
150         }
151
152         FILE* f = fopen_boost (Config::path("position"), "w");
153         if (f) {
154                 string const p = _playlists[*_selected_playlist].id()
155                         + " " + dcp::raw_convert<string>(_selected_playlist_position)
156                         + " " + dcp::raw_convert<string>(_viewer->position().get());
157
158                 fwrite (p.c_str(), p.length(), 1, f);
159                 fclose (f);
160         }
161 }
162
163 void
164 SwaroopControls::started ()
165 {
166         Controls::started ();
167         _play_button->Enable (false);
168         _pause_button->Enable (true);
169 }
170
171 void
172 SwaroopControls::stopped ()
173 {
174         Controls::stopped ();
175         _play_button->Enable (true);
176         _pause_button->Enable (false);
177 }
178
179 void
180 SwaroopControls::play_clicked ()
181 {
182         _viewer->start ();
183 }
184
185 void
186 SwaroopControls::setup_sensitivity ()
187 {
188         Controls::setup_sensitivity ();
189         bool const active_job = _active_job && *_active_job != "examine_content";
190         bool const c = _film && !_film->content().empty() && !active_job;
191         _play_button->Enable (c && !_viewer->playing());
192         _pause_button->Enable (_viewer->playing());
193         _slider->Enable (!_current_disable_timeline);
194         _spl_view->Enable (!_viewer->playing());
195         _next_button->Enable (!_current_disable_next && can_do_next());
196         _previous_button->Enable (can_do_previous());
197 }
198
199 void
200 SwaroopControls::pause_clicked ()
201 {
202         _viewer->stop ();
203 }
204
205 void
206 SwaroopControls::stop_clicked ()
207 {
208         _viewer->stop ();
209         _viewer->seek (DCPTime(), true);
210 }
211
212 bool
213 SwaroopControls::can_do_previous ()
214 {
215         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
216 }
217
218 void
219 SwaroopControls::previous_clicked ()
220 {
221         if (!can_do_previous ()) {
222                 return;
223         }
224
225         _selected_playlist_position--;
226         update_current_content ();
227 }
228
229 bool
230 SwaroopControls::can_do_next ()
231 {
232         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
233 }
234
235 void
236 SwaroopControls::next_clicked ()
237 {
238         if (!can_do_next ()) {
239                 return;
240         }
241
242         _selected_playlist_position++;
243         update_current_content ();
244 }
245
246 void
247 SwaroopControls::log (wxString s)
248 {
249         struct timeval time;
250         gettimeofday (&time, 0);
251         char buffer[64];
252         time_t const sec = time.tv_sec;
253         struct tm* t = localtime (&sec);
254         strftime (buffer, 64, "%c", t);
255         wxString ts = std_to_wx(string(buffer)) + N_(": ");
256         _log->SetValue(_log->GetValue() + ts + s + "\n");
257 }
258
259 void
260 SwaroopControls::add_playlist_to_list (SPL spl)
261 {
262         int const N = _spl_view->GetItemCount();
263
264         wxListItem it;
265         it.SetId(N);
266         it.SetColumn(0);
267         string t = spl.name();
268         if (spl.missing()) {
269                 t += " (content missing)";
270         }
271         it.SetText (std_to_wx(t));
272         _spl_view->InsertItem (it);
273 }
274
275 struct SPLComparator
276 {
277         bool operator() (SPL const & a, SPL const & b) {
278                 return a.name() < b.name();
279         }
280 };
281
282 void
283 SwaroopControls::update_playlist_directory ()
284 {
285         using namespace boost::filesystem;
286
287         _spl_view->DeleteAllItems ();
288         optional<path> dir = Config::instance()->player_playlist_directory();
289         if (!dir) {
290                 return;
291         }
292
293         _playlists.clear ();
294
295         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
296                 try {
297                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
298                                 SPL spl;
299                                 spl.read (i->path(), _content_view);
300                                 _playlists.push_back (spl);
301                         }
302                 } catch (exception& e) {
303                         /* Never mind */
304                 }
305         }
306
307         sort (_playlists.begin(), _playlists.end(), SPLComparator());
308         BOOST_FOREACH (SPL i, _playlists) {
309                 add_playlist_to_list (i);
310         }
311
312         _selected_playlist = boost::none;
313 }
314
315 void
316 SwaroopControls::spl_selection_changed ()
317 {
318         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
319         if (selected == -1) {
320                 _current_spl_view->DeleteAllItems ();
321                 _selected_playlist = boost::none;
322                 return;
323         }
324
325         if (_playlists[selected].missing()) {
326                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
327                 _selected_playlist = boost::none;
328                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
329                 return;
330         }
331
332         if (_playlists[selected].get().empty()) {
333                 error_dialog (this, "This playlist is empty.");
334                 return;
335         }
336
337         _current_spl_view->DeleteAllItems ();
338
339         int N = 0;
340         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
341                 wxListItem it;
342                 it.SetId (N);
343                 it.SetColumn (0);
344                 it.SetText (std_to_wx(i.name));
345                 _current_spl_view->InsertItem (it);
346                 ++N;
347         }
348
349         _selected_playlist = selected;
350         _selected_playlist_position = 0;
351         reset_film ();
352         update_current_content ();
353 }
354
355 void
356 SwaroopControls::reset_film ()
357 {
358         DCPOMATIC_ASSERT (_selected_playlist);
359         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
360         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
361         ResetFilm (film);
362 }
363
364 void
365 SwaroopControls::config_changed (int property)
366 {
367         Controls::config_changed (property);
368
369         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
370                 _content_view->update ();
371         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
372                 update_playlist_directory ();
373         }
374 }
375
376 void
377 SwaroopControls::set_film (shared_ptr<Film> film)
378 {
379         Controls::set_film (film);
380         setup_sensitivity ();
381 }
382
383 void
384 SwaroopControls::update_current_content ()
385 {
386         DCPOMATIC_ASSERT (_selected_playlist);
387
388         bool const was_playing = _viewer->stop ();
389
390         SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
391         _current_disable_timeline = e.disable_timeline;
392         _current_disable_next = !e.skippable;
393
394         setup_sensitivity ();
395         reset_film ();
396         if (was_playing) {
397                 _viewer->start ();
398         }
399 }
400
401 void
402 SwaroopControls::viewer_finished ()
403 {
404         if (!_selected_playlist) {
405                 return;
406         }
407
408         _selected_playlist_position++;
409         if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
410                 update_current_content ();
411         } else {
412                 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
413         }
414 }