fdc7afcabaea19e30b45b59a28a68dee0bff0e45
[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 std::list;
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::optional;
41 using boost::dynamic_pointer_cast;
42
43 ContentView::ContentView (wxWindow* parent, weak_ptr<Film> film)
44         : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER)
45         , _film (film)
46 {
47         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
48         /* type */
49         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
50         /* annotation text */
51         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
52 }
53
54 shared_ptr<Content>
55 ContentView::selected () const
56 {
57         long int s = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
58         if (s == -1) {
59                 return shared_ptr<Content>();
60         }
61
62         DCPOMATIC_ASSERT (s < int(_content.size()));
63         return _content[s];
64 }
65
66 void
67 ContentView::update ()
68 {
69         shared_ptr<Film> film = _film.lock ();
70         if (!film) {
71                 return;
72         }
73
74         using namespace boost::filesystem;
75
76         DeleteAllItems ();
77         _content.clear ();
78         optional<path> dir = Config::instance()->player_content_directory();
79         if (!dir) {
80                 return;
81         }
82
83         wxProgressDialog progress (_("DCP-o-matic"), _("Reading content directory"));
84         JobManager* jm = JobManager::instance ();
85
86         list<shared_ptr<ExamineContentJob> > jobs;
87
88         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
89                 try {
90                         shared_ptr<Content> content;
91                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
92                                 content.reset (new DCPContent(*i));
93                         } else if (i->path().extension() == ".mp4" || i->path().extension() == ".ecinema") {
94                                 content = content_factory(*i).front();
95                         }
96
97                         if (content) {
98                                 shared_ptr<ExamineContentJob> job(new ExamineContentJob(film, content));
99                                 jm->add (job);
100                                 jobs.push_back (job);
101                         }
102                 } catch (boost::filesystem::filesystem_error& e) {
103                         /* Never mind */
104                 } catch (dcp::DCPReadError& e) {
105                         /* Never mind */
106                 }
107         }
108
109         while (jm->work_to_do()) {
110                 if (!progress.Pulse()) {
111                         /* user pressed cancel */
112                         BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
113                                 i->cancel();
114                         }
115                         return;
116                 }
117                 dcpomatic_sleep (1);
118         }
119
120         /* Add content from successful jobs and report errors */
121         BOOST_FOREACH (shared_ptr<ExamineContentJob> i, jobs) {
122                 if (i->finished_in_error()) {
123                         error_dialog(this, std_to_wx(i->error_summary()) + ".\n", std_to_wx(i->error_details()));
124                 } else {
125                         add (i->content());
126                         _content.push_back (i->content());
127                 }
128         }
129 }
130
131 void
132 ContentView::add (shared_ptr<Content> content)
133 {
134         int const N = GetItemCount();
135
136         shared_ptr<Film> film = _film.lock ();
137         DCPOMATIC_ASSERT (film);
138
139         wxListItem it;
140         it.SetId(N);
141         it.SetColumn(0);
142         DCPTime length = content->length_after_trim (film);
143         int h, m, s, f;
144         length.split (24, h, m, s, f);
145         it.SetText(wxString::Format("%02d:%02d:%02d", h, m, s));
146         InsertItem(it);
147
148         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(content);
149         if (dcp && dcp->content_kind()) {
150                 it.SetId(N);
151                 it.SetColumn(1);
152                 it.SetText(std_to_wx(dcp::content_kind_to_string(*dcp->content_kind())));
153                 SetItem(it);
154         }
155
156         it.SetId(N);
157         it.SetColumn(2);
158         it.SetText(std_to_wx(content->summary()));
159         SetItem(it);
160 }
161
162 shared_ptr<Content>
163 ContentView::get (string digest) const
164 {
165         BOOST_FOREACH (shared_ptr<Content> i, _content) {
166                 if (i->digest() == digest) {
167                         return i;
168                 }
169         }
170
171         return shared_ptr<Content>();
172 }
173
174 void
175 ContentView::set_film (weak_ptr<Film> film)
176 {
177         if (_film.lock() == film.lock()) {
178                 return;
179         }
180
181         _film = film;
182         update ();
183 }