swaroop: better handling of missing content in SPLs.
[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 <wx/listctrl.h>
30 #include <wx/progdlg.h>
31
32 using std::string;
33 using std::cout;
34 using std::exception;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using boost::optional;
38
39 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
40         : Controls (parent, viewer, false)
41         , _play_button (new Button(this, _("Play")))
42         , _pause_button (new Button(this, _("Pause")))
43         , _stop_button (new Button(this, _("Stop")))
44         , _current_disable_timeline (false)
45 {
46         _button_sizer->Add (_play_button, 0, wxEXPAND);
47         _button_sizer->Add (_pause_button, 0, wxEXPAND);
48         _button_sizer->Add (_stop_button, 0, wxEXPAND);
49
50         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
51         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
52
53         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
54         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
55
56         wxFont subheading_font (*wxNORMAL_FONT);
57         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
58
59         wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
60         {
61                 wxStaticText* m = new StaticText (this, "Playlists");
62                 m->SetFont (subheading_font);
63                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
64         }
65         _refresh_spl_view = new Button (this, "Refresh");
66         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
67
68         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
69         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
70
71         _content_view = new ContentView (this);
72
73         wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
74         {
75                 wxStaticText* m = new StaticText (this, "Content");
76                 m->SetFont (subheading_font);
77                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
78         }
79         _refresh_content_view = new Button (this, "Refresh");
80         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
81
82         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
83         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
84
85         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
86         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
87         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
88         e_sizer->Add (left_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
89         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
90
91         _v_sizer->Add (e_sizer, 1, wxEXPAND);
92
93         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
94         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
95
96         _play_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
97         _pause_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
98         _stop_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
99         _spl_view->Bind     (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
100         _spl_view->Bind     (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
101         _viewer->ImageChanged.connect (boost::bind(&SwaroopControls::image_changed, this, _1));
102         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::update_playlist_directory, this));
103         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
104
105         _content_view->update ();
106         update_playlist_directory ();
107 }
108
109 void
110 SwaroopControls::started ()
111 {
112         Controls::started ();
113         _play_button->Enable (false);
114         _pause_button->Enable (true);
115 }
116
117 void
118 SwaroopControls::stopped ()
119 {
120         Controls::stopped ();
121         _play_button->Enable (true);
122         _pause_button->Enable (false);
123 }
124
125 void
126 SwaroopControls::play_clicked ()
127 {
128         _viewer->start ();
129 }
130
131 void
132 SwaroopControls::setup_sensitivity ()
133 {
134         Controls::setup_sensitivity ();
135         bool const active_job = _active_job && *_active_job != "examine_content";
136         bool const c = _film && !_film->content().empty() && !active_job;
137         _play_button->Enable (c && !_viewer->playing());
138         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
139         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
140         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && !_current_disable_timeline);
141         _spl_view->Enable (!_viewer->playing());
142 }
143
144 void
145 SwaroopControls::pause_clicked ()
146 {
147         _viewer->stop ();
148 }
149
150 void
151 SwaroopControls::stop_clicked ()
152 {
153         _viewer->stop ();
154         _viewer->seek (DCPTime(), true);
155 }
156
157 void
158 SwaroopControls::log (wxString s)
159 {
160         struct timeval time;
161         gettimeofday (&time, 0);
162         char buffer[64];
163         time_t const sec = time.tv_sec;
164         struct tm* t = localtime (&sec);
165         strftime (buffer, 64, "%c", t);
166         wxString ts = std_to_wx(string(buffer)) + N_(": ");
167         _log->SetValue(_log->GetValue() + ts + s + "\n");
168 }
169
170 void
171 SwaroopControls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
172 {
173         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
174         if (!pv) {
175                 return;
176         }
177
178         shared_ptr<Content> c = pv->content().lock();
179         if (!c) {
180                 return;
181         }
182
183         if (c == _current_content.lock()) {
184                 return;
185         }
186
187         _current_content = c;
188
189         if (_selected_playlist) {
190                 BOOST_FOREACH (SPLEntry i, _playlists[*_selected_playlist].get()) {
191                         if (i.content == c) {
192                                 _current_disable_timeline = i.disable_timeline;
193                                 setup_sensitivity ();
194                         }
195                 }
196         }
197
198         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
199         if (!dc) {
200                 return;
201         }
202
203         if (!_current_kind || *_current_kind != dc->content_kind()) {
204                 _current_kind = dc->content_kind ();
205                 setup_sensitivity ();
206         }
207 }
208
209 void
210 SwaroopControls::add_playlist_to_list (SPL spl)
211 {
212         int const N = _spl_view->GetItemCount();
213
214         wxListItem it;
215         it.SetId(N);
216         it.SetColumn(0);
217         string t = spl.name();
218         if (spl.missing()) {
219                 t += " (content missing)";
220         }
221         it.SetText (std_to_wx(t));
222         _spl_view->InsertItem (it);
223 }
224
225 void
226 SwaroopControls::update_playlist_directory ()
227 {
228         using namespace boost::filesystem;
229
230         _spl_view->DeleteAllItems ();
231         optional<path> dir = Config::instance()->player_playlist_directory();
232         if (!dir) {
233                 return;
234         }
235
236         _playlists.clear ();
237
238         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
239                 try {
240                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
241                                 SPL spl;
242                                 spl.read (i->path(), _content_view);
243                                 _playlists.push_back (spl);
244                                 add_playlist_to_list (spl);
245                         }
246                 } catch (exception& e) {
247                         /* Never mind */
248                 }
249         }
250 }
251
252 void
253 SwaroopControls::spl_selection_changed ()
254 {
255         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
256         if (selected == -1) {
257                 _current_spl_view->DeleteAllItems ();
258                 _selected_playlist = boost::none;
259                 return;
260         }
261
262         if (_playlists[selected].missing()) {
263                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
264                 _selected_playlist = boost::none;
265                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
266                 return;
267         }
268
269         wxProgressDialog* progress = new wxProgressDialog (_("DCP-o-matic"), _("Loading playlist"));
270
271         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
272         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
273                 film->add_content (i.content);
274                 if (!progress->Pulse()) {
275                         /* user pressed cancel */
276                         _selected_playlist = boost::none;
277                         _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
278                         progress->Destroy ();
279                         return;
280                 }
281         }
282
283         progress->Destroy ();
284         _current_spl_view->DeleteAllItems ();
285
286         int N = 0;
287         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
288                 wxListItem it;
289                 it.SetId (N);
290                 it.SetColumn (0);
291                 it.SetText (std_to_wx(i.name));
292                 _current_spl_view->InsertItem (it);
293                 ++N;
294         }
295
296         _selected_playlist = selected;
297         ResetFilm (film);
298 }
299
300 void
301 SwaroopControls::config_changed (int property)
302 {
303         Controls::config_changed (property);
304
305         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
306                 _content_view->update ();
307         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
308                 update_playlist_directory ();
309         }
310 }
311
312 void
313 SwaroopControls::set_film (shared_ptr<Film> film)
314 {
315         Controls::set_film (film);
316         setup_sensitivity ();
317 }