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