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