Flip SPL list and content list.
[dcpomatic.git] / src / wx / 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 "controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "playhead_to_timecode_dialog.h"
25 #include "playhead_to_frame_dialog.h"
26 #include "lib/job_manager.h"
27 #include "lib/player_video.h"
28 #include "lib/dcp_content.h"
29 #include "lib/job.h"
30 #include "lib/examine_content_job.h"
31 #include "lib/content_factory.h"
32 #include "lib/cross.h"
33 #include <dcp/dcp.h>
34 #include <dcp/cpl.h>
35 #include <dcp/reel.h>
36 #include <dcp/reel_picture_asset.h>
37 #include <wx/wx.h>
38 #include <wx/tglbtn.h>
39 #include <wx/listctrl.h>
40 #include <wx/progdlg.h>
41
42 using std::string;
43 using std::list;
44 using std::make_pair;
45 using boost::optional;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using boost::dynamic_pointer_cast;
49
50 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
51         : wxPanel (parent)
52         , _viewer (viewer)
53         , _slider_being_moved (false)
54         , _was_running_before_slider (false)
55         , _outline_content (0)
56         , _eye (0)
57         , _jump_to_selected (0)
58         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
59         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
60         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
61         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
62         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
63         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
64 #ifdef DCPOMATIC_VARIANT_SWAROOP
65         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
66         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
67         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
68 #else
69         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
70 #endif
71 {
72         _v_sizer = new wxBoxSizer (wxVERTICAL);
73         SetSizer (_v_sizer);
74
75         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
76         if (editor_controls) {
77                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
78                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
79                 _eye = new wxChoice (this, wxID_ANY);
80                 _eye->Append (_("Left"));
81                 _eye->Append (_("Right"));
82                 _eye->SetSelection (0);
83                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
84                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
85                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
86         }
87
88         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
89
90         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
91
92         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
93         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
94         left_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
95
96         _content_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
97         /* time */
98         _content_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
99         /* type */
100         _content_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
101         /* annotation text */
102         _content_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
103         left_sizer->Add (_content_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
104
105         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
106         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
107
108         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
109         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
110         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
111         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
112         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
113
114         wxBoxSizer* buttons_sizer = new wxBoxSizer (wxVERTICAL);
115         _add_button = new wxButton(this, wxID_ANY, _("Add"));
116         buttons_sizer->Add (_add_button);
117         _save_button = new wxButton(this, wxID_ANY, _("Save..."));
118         buttons_sizer->Add (_save_button);
119         _load_button = new wxButton(this, wxID_ANY, _("Load..."));
120         buttons_sizer->Add (_load_button);
121         e_sizer->Add (buttons_sizer, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
122
123         _v_sizer->Add (e_sizer, 1, wxEXPAND);
124
125         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
126         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
127
128         _content_view->Show (false);
129         _spl_view->Show (false);
130         _current_spl_view->Show (false);
131         _add_button->Show (false);
132         _save_button->Show (false);
133         _load_button->Show (false);
134         _log->Show (false);
135
136         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
137
138         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
139         time_sizer->Add (_frame_number, 0, wxEXPAND);
140         time_sizer->Add (_timecode, 0, wxEXPAND);
141
142         h_sizer->Add (_rewind_button, 0, wxALL, 2);
143         h_sizer->Add (_back_button, 0, wxALL, 2);
144         h_sizer->Add (time_sizer, 0, wxEXPAND);
145         h_sizer->Add (_forward_button, 0, wxALL, 2);
146         h_sizer->Add (_play_button, 0, wxEXPAND);
147 #ifdef DCPOMATIC_VARIANT_SWAROOP
148         h_sizer->Add (_pause_button, 0, wxEXPAND);
149         h_sizer->Add (_stop_button, 0, wxEXPAND);
150 #endif
151         h_sizer->Add (_slider, 1, wxEXPAND);
152
153         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
154
155         _frame_number->SetMinSize (wxSize (84, -1));
156         _rewind_button->SetMinSize (wxSize (32, -1));
157         _back_button->SetMinSize (wxSize (32, -1));
158         _forward_button->SetMinSize (wxSize (32, -1));
159
160         if (_eye) {
161                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
162         }
163         if (_outline_content) {
164                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
165         }
166
167         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
168         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
169         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
170         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
171 #ifdef DCPOMATIC_VARIANT_SWAROOP
172         _play_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::play_clicked,    this));
173         _pause_button->Bind     (wxEVT_BUTTON,               boost::bind(&Controls::pause_clicked,   this));
174         _stop_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::stop_clicked,    this));
175 #else
176         _play_button->Bind      (wxEVT_TOGGLEBUTTON,         boost::bind(&Controls::play_clicked,    this));
177 #endif
178         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
179         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
180         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
181         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
182         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
183         _content_view->Bind     (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&Controls::setup_sensitivity, this));
184         _content_view->Bind     (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&Controls::setup_sensitivity, this));
185         if (_jump_to_selected) {
186                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
187                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
188         }
189         _add_button->Bind       (wxEVT_BUTTON,              boost::bind(&Controls::add_clicked, this));
190         _save_button->Bind      (wxEVT_BUTTON,              boost::bind(&Controls::save_clicked, this));
191         _load_button->Bind      (wxEVT_BUTTON,              boost::bind(&Controls::load_clicked, this));
192
193         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
194         _viewer->Started.connect (boost::bind(&Controls::started, this));
195         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
196         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
197         _viewer->ImageChanged.connect (boost::bind(&Controls::image_changed, this, _1));
198
199         film_changed ();
200
201         setup_sensitivity ();
202         update_content_directory ();
203
204         JobManager::instance()->ActiveJobsChanged.connect (
205                 bind (&Controls::active_jobs_changed, this, _2)
206                 );
207
208         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
209         config_changed (Config::OTHER);
210 }
211
212 void
213 Controls::add_clicked ()
214 {
215         shared_ptr<Content> sel = selected_content ();
216         DCPOMATIC_ASSERT (sel);
217         _film->examine_and_add_content (sel);
218         bool const ok = display_progress (_("DCP-o-matic"), _("Loading DCP"));
219         if (!ok || !report_errors_from_last_job(this)) {
220                 return;
221         }
222         if (_film->content().size() == 1) {
223                 /* Put 1 frame of black at the start so when we seek to 0 we don't see anything */
224                 sel->set_position (DCPTime::from_frames(1, _film->video_frame_rate()));
225         }
226         add_content_to_list (sel, _current_spl_view);
227         setup_sensitivity ();
228 }
229
230 void
231 Controls::save_clicked ()
232 {
233         wxFileDialog* d = new wxFileDialog (
234                 this, _("Select playlist file"), wxEmptyString, wxEmptyString, wxT ("XML files (*.xml)|*.xml"),
235                 wxFD_SAVE | wxFD_OVERWRITE_PROMPT
236                 );
237
238         if (d->ShowModal() == wxID_OK) {
239                 _film->write_metadata(wx_to_std(d->GetPath()));
240         }
241
242         d->Destroy ();
243 }
244
245 void
246 Controls::load_clicked ()
247 {
248         wxFileDialog* d = new wxFileDialog (
249                 this, _("Select playlist file"), wxEmptyString, wxEmptyString, wxT ("XML files (*.xml)|*.xml")
250                 );
251
252         if (d->ShowModal() == wxID_OK) {
253                 _film->read_metadata (boost::filesystem::path(wx_to_std(d->GetPath())));
254                 _current_spl_view->DeleteAllItems ();
255                 BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
256                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(i);
257                         add_content_to_list (dcp, _current_spl_view);
258                 }
259         }
260
261         d->Destroy ();
262 }
263
264 void
265 Controls::config_changed (int property)
266 {
267         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
268                 update_content_directory ();
269         } else {
270                 setup_sensitivity ();
271         }
272 }
273
274 void
275 Controls::started ()
276 {
277 #ifdef DCPOMATIC_VARIANT_SWAROOP
278         _play_button->Enable (false);
279         _pause_button->Enable (true);
280 #else
281         _play_button->SetValue (true);
282 #endif
283         setup_sensitivity ();
284 }
285
286 void
287 Controls::stopped ()
288 {
289 #ifdef DCPOMATIC_VARIANT_SWAROOP
290         _play_button->Enable (true);
291         _pause_button->Enable (false);
292 #else
293         _play_button->SetValue (false);
294 #endif
295         setup_sensitivity ();
296 }
297
298 void
299 Controls::position_changed ()
300 {
301         if (!_slider_being_moved) {
302                 update_position_label ();
303                 update_position_slider ();
304         }
305 }
306
307 void
308 Controls::eye_changed ()
309 {
310         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
311 }
312
313 void
314 Controls::outline_content_changed ()
315 {
316         _viewer->set_outline_content (_outline_content->GetValue());
317 }
318
319 void
320 Controls::film_change (ChangeType type, Film::Property p)
321 {
322         if (type != CHANGE_TYPE_DONE) {
323                 return;
324         }
325
326         if (p == Film::CONTENT || p == Film::THREE_D) {
327                 setup_sensitivity ();
328         }
329 }
330
331 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
332 void
333 Controls::slider_moved (bool page)
334 {
335         if (!_film) {
336                 return;
337         }
338
339         if (!page && !_slider_being_moved) {
340                 /* This is the first event of a drag; stop playback for the duration of the drag */
341                 _was_running_before_slider = _viewer->stop ();
342                 _slider_being_moved = true;
343         }
344
345         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
346         t = t.round (_film->video_frame_rate());
347         /* Ensure that we hit the end of the film at the end of the slider */
348         if (t >= _film->length ()) {
349                 t = _film->length() - _viewer->one_video_frame();
350         }
351         _viewer->seek (t, false);
352         update_position_label ();
353 }
354
355 void
356 Controls::slider_released ()
357 {
358         if (_was_running_before_slider) {
359                 /* Restart after a drag */
360                 _viewer->start ();
361         }
362         _slider_being_moved = false;
363 }
364
365 void
366 Controls::play_clicked ()
367 {
368 #ifdef DCPOMATIC_VARIANT_SWAROOP
369         _viewer->start ();
370 #else
371         check_play_state ();
372 #endif
373 }
374
375
376 #ifndef DCPOMATIC_VARIANT_SWAROOP
377 void
378 Controls::check_play_state ()
379 {
380         if (!_film || _film->video_frame_rate() == 0) {
381                 return;
382         }
383
384         if (_play_button->GetValue()) {
385                 _viewer->start ();
386         } else {
387                 _viewer->stop ();
388         }
389 }
390 #endif
391
392 void
393 Controls::update_position_slider ()
394 {
395         if (!_film) {
396                 _slider->SetValue (0);
397                 return;
398         }
399
400         DCPTime const len = _film->length ();
401
402         if (len.get ()) {
403                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
404                 if (new_slider_position != _slider->GetValue()) {
405                         _slider->SetValue (new_slider_position);
406                 }
407         }
408 }
409
410 void
411 Controls::update_position_label ()
412 {
413         if (!_film) {
414                 _frame_number->SetLabel ("0");
415                 _timecode->SetLabel ("0:0:0.0");
416                 return;
417         }
418
419         double const fps = _film->video_frame_rate ();
420         /* Count frame number from 1 ... not sure if this is the best idea */
421         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
422         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
423 }
424
425 void
426 Controls::active_jobs_changed (optional<string> j)
427 {
428         _active_job = j;
429         setup_sensitivity ();
430 }
431
432 DCPTime
433 Controls::nudge_amount (wxKeyboardState& ev)
434 {
435         DCPTime amount = _viewer->one_video_frame ();
436
437         if (ev.ShiftDown() && !ev.ControlDown()) {
438                 amount = DCPTime::from_seconds (1);
439         } else if (!ev.ShiftDown() && ev.ControlDown()) {
440                 amount = DCPTime::from_seconds (10);
441         } else if (ev.ShiftDown() && ev.ControlDown()) {
442                 amount = DCPTime::from_seconds (60);
443         }
444
445         return amount;
446 }
447
448 void
449 Controls::rewind_clicked (wxMouseEvent& ev)
450 {
451         _viewer->seek (DCPTime(), true);
452         ev.Skip();
453 }
454
455 void
456 Controls::back_frame ()
457 {
458         _viewer->seek_by (-_viewer->one_video_frame(), true);
459 }
460
461 void
462 Controls::forward_frame ()
463 {
464         _viewer->seek_by (_viewer->one_video_frame(), true);
465 }
466
467 void
468 Controls::back_clicked (wxKeyboardState& ev)
469 {
470         _viewer->seek_by (-nudge_amount(ev), true);
471 }
472
473 void
474 Controls::forward_clicked (wxKeyboardState& ev)
475 {
476         _viewer->seek_by (nudge_amount(ev), true);
477 }
478
479 void
480 Controls::setup_sensitivity ()
481 {
482         /* examine content is the only job which stops the viewer working */
483         bool const active_job = _active_job && *_active_job != "examine_content";
484         bool const c = _film && !_film->content().empty() && !active_job;
485
486         _slider->Enable (c);
487         _rewind_button->Enable (c);
488         _back_button->Enable (c);
489         _forward_button->Enable (c);
490 #ifdef DCPOMATIC_VARIANT_SWAROOP
491         _play_button->Enable (c && !_viewer->playing());
492         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
493         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
494         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
495 #else
496         _play_button->Enable (c);
497 #endif
498         if (_outline_content) {
499                 _outline_content->Enable (c);
500         }
501         _frame_number->Enable (c);
502         _timecode->Enable (c);
503         if (_jump_to_selected) {
504                 _jump_to_selected->Enable (c);
505         }
506
507         if (_eye) {
508                 _eye->Enable (c && _film->three_d ());
509         }
510
511         _add_button->Enable (Config::instance()->allow_spl_editing() && static_cast<bool>(selected_content()));
512         _save_button->Enable (Config::instance()->allow_spl_editing());
513 }
514
515 shared_ptr<Content>
516 Controls::selected_content () const
517 {
518         long int s = _content_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
519         if (s == -1) {
520                 return shared_ptr<Content>();
521         }
522
523         DCPOMATIC_ASSERT (s < int(_content.size()));
524         return _content[s];
525 }
526
527 void
528 Controls::timecode_clicked ()
529 {
530         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
531         if (dialog->ShowModal() == wxID_OK) {
532                 _viewer->seek (dialog->get(), true);
533         }
534         dialog->Destroy ();
535 }
536
537 void
538 Controls::frame_number_clicked ()
539 {
540         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
541         if (dialog->ShowModal() == wxID_OK) {
542                 _viewer->seek (dialog->get(), true);
543         }
544         dialog->Destroy ();
545 }
546
547 void
548 Controls::jump_to_selected_clicked ()
549 {
550         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
551 }
552
553 void
554 Controls::film_changed ()
555 {
556         shared_ptr<Film> film = _viewer->film ();
557
558         if (_film == film) {
559                 return;
560         }
561
562         _film = film;
563
564         setup_sensitivity ();
565
566         update_position_slider ();
567         update_position_label ();
568
569         if (_film) {
570                 _film->Change.connect (boost::bind (&Controls::film_change, this, _1, _2));
571         }
572 }
573
574 shared_ptr<Film>
575 Controls::film () const
576 {
577         return _film;
578 }
579
580 void
581 Controls::show_extended_player_controls (bool s)
582 {
583         _content_view->Show (s);
584         if (s) {
585                 update_content_directory ();
586         }
587         _spl_view->Show (s);
588         _current_spl_view->Show (s);
589         _log->Show (s);
590         _add_button->Show (s);
591         _save_button->Show (s);
592         _load_button->Show (s);
593         _v_sizer->Layout ();
594 }
595
596 void
597 Controls::add_content_to_list (shared_ptr<Content> content, wxListCtrl* ctrl)
598 {
599         int const N = ctrl->GetItemCount();
600
601         wxListItem it;
602         it.SetId(N);
603         it.SetColumn(0);
604         DCPTime length = content->length_after_trim ();
605         int seconds = length.seconds();
606         int minutes = seconds / 60;
607         seconds -= minutes * 60;
608         int hours = minutes / 60;
609         minutes -= hours * 60;
610         it.SetText(wxString::Format("%02d:%02d:%02d", hours, minutes, seconds));
611         ctrl->InsertItem(it);
612
613         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(content);
614         if (dcp && dcp->content_kind()) {
615                 it.SetId(N);
616                 it.SetColumn(1);
617                 it.SetText(std_to_wx(dcp::content_kind_to_string(*dcp->content_kind())));
618                 ctrl->SetItem(it);
619         }
620
621         it.SetId(N);
622         it.SetColumn(2);
623         it.SetText(std_to_wx(content->summary()));
624         ctrl->SetItem(it);
625 }
626
627 void
628 Controls::update_content_directory ()
629 {
630         if (!_content_view->IsShown()) {
631                 return;
632         }
633
634         using namespace boost::filesystem;
635
636         _content_view->DeleteAllItems ();
637         _content.clear ();
638         optional<path> dir = Config::instance()->player_content_directory();
639         if (!dir) {
640                 return;
641         }
642
643         wxProgressDialog progress (_("DCP-o-matic"), _("Reading content directory"));
644         JobManager* jm = JobManager::instance ();
645
646         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
647                 try {
648                         shared_ptr<Content> content;
649                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
650                                 content.reset (new DCPContent(_film, *i));
651                         } else if (i->path().extension() == ".mp4") {
652                                 content = content_factory(_film, *i).front();
653                         }
654
655                         if (content) {
656                                 jm->add (shared_ptr<Job>(new ExamineContentJob(_film, content)));
657                                 while (jm->work_to_do()) {
658                                         if (!progress.Pulse()) {
659                                                 /* user pressed cancel */
660                                                 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
661                                                         i->cancel();
662                                                 }
663                                                 return;
664                                         }
665                                         dcpomatic_sleep (1);
666                                 }
667                                 if (report_errors_from_last_job (this)) {
668                                         add_content_to_list (content, _content_view);
669                                         _content.push_back (content);
670                                 }
671                         }
672                 } catch (boost::filesystem::filesystem_error& e) {
673                         /* Never mind */
674                 } catch (dcp::DCPReadError& e) {
675                         /* Never mind */
676                 }
677         }
678 }
679
680 #ifdef DCPOMATIC_VARIANT_SWAROOP
681 void
682 Controls::pause_clicked ()
683 {
684         _viewer->stop ();
685 }
686
687 void
688 Controls::stop_clicked ()
689 {
690         _viewer->stop ();
691         _viewer->seek (DCPTime(), true);
692 }
693 #endif
694
695 void
696 Controls::log (wxString s)
697 {
698         struct timeval time;
699         gettimeofday (&time, 0);
700         char buffer[64];
701         time_t const sec = time.tv_sec;
702         struct tm* t = localtime (&sec);
703         strftime (buffer, 64, "%c", t);
704         wxString ts = std_to_wx(string(buffer)) + N_(": ");
705         _log->SetValue(_log->GetValue() + ts + s + "\n");
706 }
707
708 void
709 Controls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
710 {
711 #ifdef DCPOMATIC_VARIANT_SWAROOP
712         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
713         if (!pv) {
714                 return;
715         }
716
717         shared_ptr<Content> c = pv->content().lock();
718         if (!c) {
719                 return;
720         }
721
722         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
723         if (!dc) {
724                 return;
725         }
726
727         if (!_current_kind || *_current_kind != dc->content_kind()) {
728                 _current_kind = dc->content_kind ();
729                 setup_sensitivity ();
730         }
731 #endif
732 }