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