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;
39 using boost::shared_ptr;
40 using libdcp::raw_convert;
42 int const ContentProperty::PATH = 400;
43 int const ContentProperty::POSITION = 401;
44 int const ContentProperty::LENGTH = 402;
45 int const ContentProperty::TRIM_START = 403;
46 int const ContentProperty::TRIM_END = 404;
48 Content::Content (shared_ptr<const Film> f)
53 , _change_signals_frequent (false)
58 Content::Content (shared_ptr<const Film> f, Time p)
63 , _change_signals_frequent (false)
68 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
73 , _change_signals_frequent (false)
78 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
80 , _change_signals_frequent (false)
82 list<cxml::NodePtr> path_children = node->node_children ("Path");
83 for (list<cxml::NodePtr>::const_iterator i = path_children.begin(); i != path_children.end(); ++i) {
84 _paths.push_back ((*i)->content ());
86 _digest = node->string_child ("Digest");
87 _position = node->number_child<Time> ("Position");
88 _trim_start = node->number_child<Time> ("TrimStart");
89 _trim_end = node->number_child<Time> ("TrimEnd");
92 Content::Content (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
94 , _position (c.front()->position ())
95 , _trim_start (c.front()->trim_start ())
96 , _trim_end (c.back()->trim_end ())
97 , _change_signals_frequent (false)
99 for (size_t i = 0; i < c.size(); ++i) {
100 if (i > 0 && c[i]->trim_start ()) {
101 throw JoinError (_("Only the first piece of content to be joined can have a start trim."));
104 if (i < (c.size() - 1) && c[i]->trim_end ()) {
105 throw JoinError (_("Only the last piece of content to be joined can have an end trim."));
108 for (size_t j = 0; j < c[i]->number_of_paths(); ++j) {
109 _paths.push_back (c[i]->path (j));
115 Content::as_xml (xmlpp::Node* node) const
117 boost::mutex::scoped_lock lm (_mutex);
119 for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
120 node->add_child("Path")->add_child_text (i->string ());
122 node->add_child("Digest")->add_child_text (_digest);
123 node->add_child("Position")->add_child_text (raw_convert<string> (_position));
124 node->add_child("TrimStart")->add_child_text (raw_convert<string> (_trim_start));
125 node->add_child("TrimEnd")->add_child_text (raw_convert<string> (_trim_end));
129 Content::examine (shared_ptr<Job> job)
131 boost::mutex::scoped_lock lm (_mutex);
132 vector<boost::filesystem::path> p = _paths;
135 string const d = md5_digest (p, job);
142 Content::signal_changed (int p)
145 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
150 Content::set_position (Time p)
153 boost::mutex::scoped_lock lm (_mutex);
154 if (p == _position) {
161 signal_changed (ContentProperty::POSITION);
165 Content::set_trim_start (Time t)
168 boost::mutex::scoped_lock lm (_mutex);
172 signal_changed (ContentProperty::TRIM_START);
176 Content::set_trim_end (Time t)
179 boost::mutex::scoped_lock lm (_mutex);
183 signal_changed (ContentProperty::TRIM_END);
188 Content::clone () const
190 shared_ptr<const Film> film = _film.lock ();
192 return shared_ptr<Content> ();
195 /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
197 xmlpp::Node* node = doc.create_root_node ("Content");
200 /* notes is unused here (we assume) */
202 return content_factory (film, cxml::NodePtr (new cxml::Node (node)), Film::current_state_version, notes);
206 Content::technical_summary () const
208 return String::compose ("%1 %2 %3", path_summary(), digest(), position());
212 Content::length_after_trim () const
214 return full_length() - trim_start() - trim_end();
217 /** @param t A time relative to the start of this content (not the position).
218 * @return true if this time is trimmed by our trim settings.
221 Content::trimmed (Time t) const
223 return (t < trim_start() || t > (full_length() - trim_end ()));
226 /** @return string which includes everything about how this content affects
230 Content::identifier () const
234 s << Content::digest()
236 << "_" << trim_start()
237 << "_" << trim_end();
243 Content::paths_valid () const
245 for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
246 if (!boost::filesystem::exists (*i)) {
255 Content::set_path (boost::filesystem::path path)
258 _paths.push_back (path);
259 signal_changed (ContentProperty::PATH);
263 Content::path_summary () const
265 /* XXX: should handle multiple paths more gracefully */
267 assert (number_of_paths ());
269 string s = path(0).filename().string ();
270 if (number_of_paths() > 1) {