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