swaroop: fix load of more than one playlist.
[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 "lib/player_video.h"
26 #include "lib/dcp_content.h"
27 #include <wx/listctrl.h>
28
29 using std::string;
30 using std::cout;
31 using std::exception;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::optional;
35
36 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
37         : Controls (parent, viewer, false)
38         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
39         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
40         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
41 {
42         _button_sizer->Add (_play_button, 0, wxEXPAND);
43         _button_sizer->Add (_pause_button, 0, wxEXPAND);
44         _button_sizer->Add (_stop_button, 0, wxEXPAND);
45
46         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
47         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
48
49         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
50         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
51
52         left_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
53
54         _content_view = new ContentView (this);
55         left_sizer->Add (_content_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
56
57         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
58         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
59         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
60         e_sizer->Add (left_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
61         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
62
63         _v_sizer->Add (e_sizer, 1, wxEXPAND);
64
65         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
66         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
67
68         _play_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
69         _pause_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
70         _stop_button->Bind  (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
71         _spl_view->Bind     (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
72         _spl_view->Bind     (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
73         _viewer->ImageChanged.connect (boost::bind(&SwaroopControls::image_changed, this, _1));
74
75         _content_view->update ();
76         update_playlist_directory ();
77 }
78
79 void
80 SwaroopControls::started ()
81 {
82         Controls::started ();
83         _play_button->Enable (false);
84         _pause_button->Enable (true);
85 }
86
87 void
88 SwaroopControls::stopped ()
89 {
90         Controls::stopped ();
91         _play_button->Enable (true);
92         _pause_button->Enable (false);
93 }
94
95 void
96 SwaroopControls::play_clicked ()
97 {
98         _viewer->start ();
99 }
100
101 void
102 SwaroopControls::setup_sensitivity ()
103 {
104         Controls::setup_sensitivity ();
105         bool const active_job = _active_job && *_active_job != "examine_content";
106         bool const c = _film && !_film->content().empty() && !active_job;
107         _play_button->Enable (c && !_viewer->playing());
108         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
109         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
110         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
111 }
112
113 void
114 SwaroopControls::pause_clicked ()
115 {
116         _viewer->stop ();
117 }
118
119 void
120 SwaroopControls::stop_clicked ()
121 {
122         _viewer->stop ();
123         _viewer->seek (DCPTime(), true);
124 }
125
126 void
127 SwaroopControls::log (wxString s)
128 {
129         struct timeval time;
130         gettimeofday (&time, 0);
131         char buffer[64];
132         time_t const sec = time.tv_sec;
133         struct tm* t = localtime (&sec);
134         strftime (buffer, 64, "%c", t);
135         wxString ts = std_to_wx(string(buffer)) + N_(": ");
136         _log->SetValue(_log->GetValue() + ts + s + "\n");
137 }
138
139 void
140 SwaroopControls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
141 {
142         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
143         if (!pv) {
144                 return;
145         }
146
147         shared_ptr<Content> c = pv->content().lock();
148         if (!c) {
149                 return;
150         }
151
152         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
153         if (!dc) {
154                 return;
155         }
156
157         if (!_current_kind || *_current_kind != dc->content_kind()) {
158                 _current_kind = dc->content_kind ();
159                 setup_sensitivity ();
160         }
161 }
162
163 void
164 SwaroopControls::add_playlist_to_list (SPL spl)
165 {
166         int const N = _spl_view->GetItemCount();
167
168         wxListItem it;
169         it.SetId(N);
170         it.SetColumn(0);
171         it.SetText (std_to_wx(spl.name()));
172         _spl_view->InsertItem (it);
173 }
174
175 void
176 SwaroopControls::update_playlist_directory ()
177 {
178         using namespace boost::filesystem;
179
180         _spl_view->DeleteAllItems ();
181         optional<path> dir = Config::instance()->player_playlist_directory();
182         if (!dir) {
183                 return;
184         }
185
186         _playlists.clear ();
187
188         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
189                 try {
190                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
191                                 SPL spl;
192                                 spl.read (i->path(), _content_view);
193                                 _playlists.push_back (spl);
194                                 add_playlist_to_list (spl);
195                         }
196                 } catch (exception& e) {
197                         /* Never mind */
198                 }
199         }
200 }
201
202 void
203 SwaroopControls::spl_selection_changed ()
204 {
205         _current_spl_view->DeleteAllItems ();
206
207         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
208         if (selected == -1) {
209                 return;
210         }
211
212         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
213
214         int N = 0;
215         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
216                 wxListItem it;
217                 it.SetId (N);
218                 it.SetColumn (0);
219                 it.SetText (std_to_wx(i.name));
220                 _current_spl_view->InsertItem (it);
221                 film->add_content (i.content);
222                 ++N;
223         }
224
225         ResetFilm (film);
226 }
227
228 void
229 SwaroopControls::config_changed (int property)
230 {
231         Controls::config_changed (property);
232
233         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
234                 _content_view->update ();
235         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
236                 update_playlist_directory ();
237         }
238 }
239
240 void
241 SwaroopControls::set_film (shared_ptr<Film> film)
242 {
243         Controls::set_film (film);
244         update_playlist_directory ();
245         setup_sensitivity ();
246 }