Make MovingImageContent use Content::_paths rather than its own _files list.
[dcpomatic.git] / src / lib / content.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/thread/mutex.hpp>
21 #include <libxml++/libxml++.h>
22 #include <libcxml/cxml.h>
23 #include "content.h"
24 #include "util.h"
25 #include "content_factory.h"
26 #include "ui_signaller.h"
27
28 using std::string;
29 using std::stringstream;
30 using std::set;
31 using boost::shared_ptr;
32 using boost::lexical_cast;
33
34 int const ContentProperty::PATH = 400;
35 int const ContentProperty::POSITION = 401;
36 int const ContentProperty::LENGTH = 402;
37 int const ContentProperty::TRIM_START = 403;
38 int const ContentProperty::TRIM_END = 404;
39
40 Content::Content (shared_ptr<const Film> f)
41         : _film (f)
42         , _position (0)
43         , _trim_start (0)
44         , _trim_end (0)
45         , _change_signals_frequent (false)
46 {
47
48 }
49
50 Content::Content (shared_ptr<const Film> f, Time p)
51         : _film (f)
52         , _position (p)
53         , _trim_start (0)
54         , _trim_end (0)
55         , _change_signals_frequent (false)
56 {
57
58 }
59
60 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
61         : _film (f)
62         , _position (0)
63         , _trim_start (0)
64         , _trim_end (0)
65         , _change_signals_frequent (false)
66 {
67         _paths.push_back (p);
68 }
69
70 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
71         : _film (f)
72         , _change_signals_frequent (false)
73 {
74         _paths.push_back (node->string_child ("Path"));
75         _digest = node->string_child ("Digest");
76         _position = node->number_child<Time> ("Position");
77         _trim_start = node->number_child<Time> ("TrimStart");
78         _trim_end = node->number_child<Time> ("TrimEnd");
79 }
80
81 void
82 Content::as_xml (xmlpp::Node* node) const
83 {
84         boost::mutex::scoped_lock lm (_mutex);
85         
86         node->add_child("Path")->add_child_text (_paths.front().string());
87         node->add_child("Digest")->add_child_text (_digest);
88         node->add_child("Position")->add_child_text (lexical_cast<string> (_position));
89         node->add_child("TrimStart")->add_child_text (lexical_cast<string> (_trim_start));
90         node->add_child("TrimEnd")->add_child_text (lexical_cast<string> (_trim_end));
91 }
92
93 void
94 Content::examine (shared_ptr<Job> job)
95 {
96         boost::mutex::scoped_lock lm (_mutex);
97         boost::filesystem::path p = _paths.front ();
98         lm.unlock ();
99         
100         string d;
101         if (boost::filesystem::is_regular_file (p)) {
102                 d = md5_digest (p);
103         } else {
104                 d = md5_digest_directory (p, job);
105         }
106
107         lm.lock ();
108         _digest = d;
109 }
110
111 void
112 Content::signal_changed (int p)
113 {
114         if (ui_signaller) {
115                 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
116         }
117 }
118
119 void
120 Content::set_position (Time p)
121 {
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 _position = p;
125         }
126
127         signal_changed (ContentProperty::POSITION);
128 }
129
130 void
131 Content::set_trim_start (Time t)
132 {
133         {
134                 boost::mutex::scoped_lock lm (_mutex);
135                 _trim_start = t;
136         }
137
138         signal_changed (ContentProperty::TRIM_START);
139 }
140
141 void
142 Content::set_trim_end (Time t)
143 {
144         {
145                 boost::mutex::scoped_lock lm (_mutex);
146                 _trim_end = t;
147         }
148
149         signal_changed (ContentProperty::TRIM_END);
150 }
151
152
153 shared_ptr<Content>
154 Content::clone () const
155 {
156         shared_ptr<const Film> film = _film.lock ();
157         if (!film) {
158                 return shared_ptr<Content> ();
159         }
160         
161         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
162         xmlpp::Document doc;
163         xmlpp::Node* node = doc.create_root_node ("Content");
164         as_xml (node);
165         return content_factory (film, cxml::NodePtr(new cxml::Node (node)));
166 }
167
168 string
169 Content::technical_summary () const
170 {
171         return String::compose ("%1 %2 %3", path(), digest(), position());
172 }
173
174 Time
175 Content::length_after_trim () const
176 {
177         return full_length() - trim_start() - trim_end();
178 }
179
180 /** @param t A time relative to the start of this content (not the position).
181  *  @return true if this time is trimmed by our trim settings.
182  */
183 bool
184 Content::trimmed (Time t) const
185 {
186         return (t < trim_start() || t > (full_length() - trim_end ()));
187 }
188
189 /** @return string which includes everything about how this content affects
190  *  its playlist.
191  */
192 string
193 Content::identifier () const
194 {
195         stringstream s;
196         
197         s << Content::digest()
198           << "_" << position()
199           << "_" << trim_start()
200           << "_" << trim_end();
201
202         return s.str ();
203 }
204
205 bool
206 Content::path_valid () const
207 {
208         return boost::filesystem::exists (_paths.front ());
209 }
210
211 void
212 Content::set_path (boost::filesystem::path path)
213 {
214         _paths.clear ();
215         _paths.push_back (path);
216         signal_changed (ContentProperty::PATH);
217 }
218
219