Fix content list in player.
[dcpomatic.git] / src / wx / content_view.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 "content_view.h"
22 #include "wx_util.h"
23 #include "lib/dcpomatic_assert.h"
24 #include "lib/config.h"
25 #include "lib/dcp_content.h"
26 #include "lib/content_factory.h"
27 #include "lib/examine_content_job.h"
28 #include "lib/job_manager.h"
29 #include "lib/cross.h"
30 #include <dcp/exceptions.h>
31 #include <boost/filesystem.hpp>
32 #include <boost/optional.hpp>
33 #include <wx/progdlg.h>
34
35 using std::string;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::optional;
40 using boost::dynamic_pointer_cast;
41
42 ContentView::ContentView (wxWindow* parent, weak_ptr<Film> film)
43         : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER)
44         , _film (film)
45 {
46         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
47         /* type */
48         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
49         /* annotation text */
50         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
51 }
52
53 shared_ptr<Content>
54 ContentView::selected () const
55 {
56         long int s = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
57         if (s == -1) {
58                 return shared_ptr<Content>();
59         }
60
61         DCPOMATIC_ASSERT (s < int(_content.size()));
62         return _content[s];
63 }
64
65 void
66 ContentView::update ()
67 {
68         shared_ptr<Film> film = _film.lock ();
69         if (!film) {
70                 return;
71         }
72
73         using namespace boost::filesystem;
74
75         DeleteAllItems ();
76         _content.clear ();
77         optional<path> dir = Config::instance()->player_content_directory();
78         if (!dir) {
79                 return;
80         }
81
82         wxProgressDialog progress (_("DCP-o-matic"), _("Reading content directory"));
83         JobManager* jm = JobManager::instance ();
84
85         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
86                 try {
87                         shared_ptr<Content> content;
88                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
89                                 content.reset (new DCPContent(film, *i));
90                         } else if (i->path().extension() == ".mp4" || i->path().extension() == ".ecinema") {
91                                 content = content_factory(film, *i).front();
92                         }
93
94                         if (content) {
95                                 jm->add (shared_ptr<Job>(new ExamineContentJob(film, content)));
96                                 while (jm->work_to_do()) {
97                                         if (!progress.Pulse()) {
98                                                 /* user pressed cancel */
99                                                 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
100                                                         i->cancel();
101                                                 }
102                                                 return;
103                                         }
104                                         dcpomatic_sleep (1);
105                                 }
106                                 if (report_errors_from_last_job (this)) {
107                                         add (content);
108                                         _content.push_back (content);
109                                 }
110                         }
111                 } catch (boost::filesystem::filesystem_error& e) {
112                         /* Never mind */
113                 } catch (dcp::DCPReadError& e) {
114                         /* Never mind */
115                 }
116         }
117 }
118
119 void
120 ContentView::add (shared_ptr<Content> content)
121 {
122         int const N = GetItemCount();
123
124         wxListItem it;
125         it.SetId(N);
126         it.SetColumn(0);
127         DCPTime length = content->length_after_trim ();
128         int h, m, s, f;
129         length.split (24, h, m, s, f);
130         it.SetText(wxString::Format("%02d:%02d:%02d", h, m, s));
131         InsertItem(it);
132
133         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(content);
134         if (dcp && dcp->content_kind()) {
135                 it.SetId(N);
136                 it.SetColumn(1);
137                 it.SetText(std_to_wx(dcp::content_kind_to_string(*dcp->content_kind())));
138                 SetItem(it);
139         }
140
141         it.SetId(N);
142         it.SetColumn(2);
143         it.SetText(std_to_wx(content->summary()));
144         SetItem(it);
145 }
146
147 shared_ptr<Content>
148 ContentView::get (string digest) const
149 {
150         BOOST_FOREACH (shared_ptr<Content> i, _content) {
151                 if (i->digest() == digest) {
152                         return i;
153                 }
154         }
155
156         return shared_ptr<Content>();
157 }
158
159 void
160 ContentView::set_film (weak_ptr<Film> film)
161 {
162         _film = film;
163 }