BOOST_FOREACH.
[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 std::shared_ptr;
39 using std::weak_ptr;
40 using boost::optional;
41 using std::dynamic_pointer_cast;
42 using namespace dcpomatic;
43
44 ContentView::ContentView (wxWindow* parent)
45         : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER)
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         using namespace boost::filesystem;
70
71         DeleteAllItems ();
72         _content.clear ();
73         optional<path> dir = Config::instance()->player_content_directory();
74         if (!dir || !boost::filesystem::is_directory(*dir)) {
75                 dir = home_directory ();
76         }
77
78         wxProgressDialog progress (_("DCP-o-matic"), _("Reading content directory"));
79         JobManager* jm = JobManager::instance ();
80
81         list<shared_ptr<ExamineContentJob> > jobs;
82
83         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
84                 try {
85                         progress.Pulse ();
86
87                         shared_ptr<Content> content;
88                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
89                                 content.reset (new DCPContent(*i));
90                         } else if (i->path().extension() == ".mp4" || i->path().extension() == ".ecinema") {
91                                 content = content_factory(*i).front();
92                         }
93
94                         if (content) {
95                                 shared_ptr<ExamineContentJob> job(new ExamineContentJob(shared_ptr<Film>(), content));
96                                 jm->add (job);
97                                 jobs.push_back (job);
98                         }
99                 } catch (boost::filesystem::filesystem_error& e) {
100                         /* Never mind */
101                 } catch (dcp::ReadError& e) {
102                         /* Never mind */
103                 }
104         }
105
106         while (jm->work_to_do()) {
107                 if (!progress.Pulse()) {
108                         /* user pressed cancel */
109                         for (auto i: jm->get()) {
110                                 i->cancel();
111                         }
112                         return;
113                 }
114                 dcpomatic_sleep_seconds (1);
115         }
116
117         /* Add content from successful jobs and report errors */
118         for (auto i: jobs) {
119                 if (i->finished_in_error()) {
120                         error_dialog(this, std_to_wx(i->error_summary()) + ".\n", std_to_wx(i->error_details()));
121                 } else {
122                         add (i->content());
123                         _content.push_back (i->content());
124                 }
125         }
126 }
127
128 void
129 ContentView::add (shared_ptr<Content> content)
130 {
131         int const N = GetItemCount();
132
133         wxListItem it;
134         it.SetId(N);
135         it.SetColumn(0);
136         DCPTime length = content->approximate_length ();
137         int h, m, s, f;
138         length.split (24, h, m, s, f);
139         it.SetText(wxString::Format("%02d:%02d:%02d", h, m, s));
140         InsertItem(it);
141
142         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(content);
143         if (dcp && dcp->content_kind()) {
144                 it.SetId(N);
145                 it.SetColumn(1);
146                 it.SetText(std_to_wx(dcp::content_kind_to_string(*dcp->content_kind())));
147                 SetItem(it);
148         }
149
150         it.SetId(N);
151         it.SetColumn(2);
152         it.SetText(std_to_wx(content->summary()));
153         SetItem(it);
154 }
155
156 shared_ptr<Content>
157 ContentView::get (string digest) const
158 {
159         for (auto i: _content) {
160                 if (i->digest() == digest) {
161                         return i;
162                 }
163         }
164
165         return shared_ptr<Content>();
166 }