2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <boost/thread/mutex.hpp>
21 #include <libxml++/libxml++.h>
22 #include <libcxml/cxml.h>
23 #include <libdcp/raw_convert.h>
26 #include "content_factory.h"
27 #include "ui_signaller.h"
28 #include "exceptions.h"
34 using std::stringstream;
40 using boost::shared_ptr;
41 using libdcp::raw_convert;
43 int const ContentProperty::PATH = 400;
44 int const ContentProperty::POSITION = 401;
45 int const ContentProperty::LENGTH = 402;
46 int const ContentProperty::TRIM_START = 403;
47 int const ContentProperty::TRIM_END = 404;
49 Content::Content (shared_ptr<const Film> f)
54 , _change_signals_frequent (false)
59 Content::Content (shared_ptr<const Film> f, Time p)
64 , _change_signals_frequent (false)
69 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
74 , _change_signals_frequent (false)
79 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
81 , _change_signals_frequent (false)
83 list<cxml::NodePtr> path_children = node->node_children ("Path");
84 for (list<cxml::NodePtr>::const_iterator i = path_children.begin(); i != path_children.end(); ++i) {
85 _paths.push_back ((*i)->content ());
87 _digest = node->string_child ("Digest");
88 _position = node->number_child<Time> ("Position");
89 _trim_start = node->number_child<Time> ("TrimStart");
90 _trim_end = node->number_child<Time> ("TrimEnd");
93 Content::Content (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
95 , _position (c.front()->position ())
96 , _trim_start (c.front()->trim_start ())
97 , _trim_end (c.back()->trim_end ())
98 , _change_signals_frequent (false)
100 for (size_t i = 0; i < c.size(); ++i) {
101 if (i > 0 && c[i]->trim_start ()) {
102 throw JoinError (_("Only the first piece of content to be joined can have a start trim."));
105 if (i < (c.size() - 1) && c[i]->trim_end ()) {
106 throw JoinError (_("Only the last piece of content to be joined can have an end trim."));
109 for (size_t j = 0; j < c[i]->number_of_paths(); ++j) {
110 _paths.push_back (c[i]->path (j));
116 Content::as_xml (xmlpp::Node* node) const
118 boost::mutex::scoped_lock lm (_mutex);
120 for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
121 node->add_child("Path")->add_child_text (i->string ());
123 node->add_child("Digest")->add_child_text (_digest);
124 node->add_child("Position")->add_child_text (raw_convert<string> (_position));
125 node->add_child("TrimStart")->add_child_text (raw_convert<string> (_trim_start));
126 node->add_child("TrimEnd")->add_child_text (raw_convert<string> (_trim_end));
130 Content::examine (shared_ptr<Job> job)
132 boost::mutex::scoped_lock lm (_mutex);
133 vector<boost::filesystem::path> p = _paths;
136 string const d = md5_digest (p, job);
143 Content::signal_changed (int p)
146 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
151 Content::set_position (Time p)
154 boost::mutex::scoped_lock lm (_mutex);
155 if (p == _position) {
162 signal_changed (ContentProperty::POSITION);
166 Content::set_trim_start (Time t)
169 boost::mutex::scoped_lock lm (_mutex);
173 signal_changed (ContentProperty::TRIM_START);
177 Content::set_trim_end (Time t)
180 boost::mutex::scoped_lock lm (_mutex);
184 signal_changed (ContentProperty::TRIM_END);
189 Content::clone () const
191 shared_ptr<const Film> film = _film.lock ();
193 return shared_ptr<Content> ();
196 /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
198 xmlpp::Node* node = doc.create_root_node ("Content");
201 /* notes is unused here (we assume) */
203 return content_factory (film, cxml::NodePtr (new cxml::Node (node)), Film::current_state_version, notes);
207 Content::technical_summary () const
209 return String::compose ("%1 %2 %3", path_summary(), digest(), position());
213 Content::length_after_trim () const
215 return max (int64_t (0), full_length() - trim_start() - trim_end());
218 /** @param t A time relative to the start of this content (not the position).
219 * @return true if this time is trimmed by our trim settings.
222 Content::trimmed (Time t) const
224 return (t < trim_start() || t > (full_length() - trim_end ()));
227 /** @return string which includes everything about how this content affects
231 Content::identifier () const
235 s << Content::digest()
237 << "_" << trim_start()
238 << "_" << trim_end();
244 Content::paths_valid () const
246 for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
247 if (!boost::filesystem::exists (*i)) {
256 Content::set_path (boost::filesystem::path path)
259 _paths.push_back (path);
260 signal_changed (ContentProperty::PATH);
264 Content::path_summary () const
266 /* XXX: should handle multiple paths more gracefully */
268 assert (number_of_paths ());
270 string s = path(0).filename().string ();
271 if (number_of_paths() > 1) {