Remove LocaleGuard and lexical_cast<> in favour of libdcp::raw_convert,
[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 <libdcp/raw_convert.h>
24 #include "content.h"
25 #include "util.h"
26 #include "content_factory.h"
27 #include "ui_signaller.h"
28 #include "exceptions.h"
29 #include "film.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::stringstream;
35 using std::set;
36 using std::list;
37 using std::cout;
38 using std::vector;
39 using boost::shared_ptr;
40 using libdcp::raw_convert;
41
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;
47
48 Content::Content (shared_ptr<const Film> f)
49         : _film (f)
50         , _position (0)
51         , _trim_start (0)
52         , _trim_end (0)
53         , _change_signals_frequent (false)
54 {
55
56 }
57
58 Content::Content (shared_ptr<const Film> f, Time p)
59         : _film (f)
60         , _position (p)
61         , _trim_start (0)
62         , _trim_end (0)
63         , _change_signals_frequent (false)
64 {
65
66 }
67
68 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
69         : _film (f)
70         , _position (0)
71         , _trim_start (0)
72         , _trim_end (0)
73         , _change_signals_frequent (false)
74 {
75         _paths.push_back (p);
76 }
77
78 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
79         : _film (f)
80         , _change_signals_frequent (false)
81 {
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 ());
85         }
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");
90 }
91
92 Content::Content (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
93         : _film (f)
94         , _position (c.front()->position ())
95         , _trim_start (c.front()->trim_start ())
96         , _trim_end (c.back()->trim_end ())
97         , _change_signals_frequent (false)
98 {
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."));
102                 }
103
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."));
106                 }
107
108                 for (size_t j = 0; j < c[i]->number_of_paths(); ++j) {
109                         _paths.push_back (c[i]->path (j));
110                 }
111         }
112 }
113
114 void
115 Content::as_xml (xmlpp::Node* node) const
116 {
117         boost::mutex::scoped_lock lm (_mutex);
118
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 ());
121         }
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));
126 }
127
128 void
129 Content::examine (shared_ptr<Job> job)
130 {
131         boost::mutex::scoped_lock lm (_mutex);
132         vector<boost::filesystem::path> p = _paths;
133         lm.unlock ();
134         
135         string const d = md5_digest (p, job);
136
137         lm.lock ();
138         _digest = d;
139 }
140
141 void
142 Content::signal_changed (int p)
143 {
144         if (ui_signaller) {
145                 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
146         }
147 }
148
149 void
150 Content::set_position (Time p)
151 {
152         {
153                 boost::mutex::scoped_lock lm (_mutex);
154                 if (p == _position) {
155                         return;
156                 }
157                 
158                 _position = p;
159         }
160
161         signal_changed (ContentProperty::POSITION);
162 }
163
164 void
165 Content::set_trim_start (Time t)
166 {
167         {
168                 boost::mutex::scoped_lock lm (_mutex);
169                 _trim_start = t;
170         }
171
172         signal_changed (ContentProperty::TRIM_START);
173 }
174
175 void
176 Content::set_trim_end (Time t)
177 {
178         {
179                 boost::mutex::scoped_lock lm (_mutex);
180                 _trim_end = t;
181         }
182
183         signal_changed (ContentProperty::TRIM_END);
184 }
185
186
187 shared_ptr<Content>
188 Content::clone () const
189 {
190         shared_ptr<const Film> film = _film.lock ();
191         if (!film) {
192                 return shared_ptr<Content> ();
193         }
194         
195         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
196         xmlpp::Document doc;
197         xmlpp::Node* node = doc.create_root_node ("Content");
198         as_xml (node);
199
200         /* notes is unused here (we assume) */
201         list<string> notes;
202         return content_factory (film, cxml::NodePtr (new cxml::Node (node)), Film::current_state_version, notes);
203 }
204
205 string
206 Content::technical_summary () const
207 {
208         return String::compose ("%1 %2 %3", path_summary(), digest(), position());
209 }
210
211 Time
212 Content::length_after_trim () const
213 {
214         return full_length() - trim_start() - trim_end();
215 }
216
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.
219  */
220 bool
221 Content::trimmed (Time t) const
222 {
223         return (t < trim_start() || t > (full_length() - trim_end ()));
224 }
225
226 /** @return string which includes everything about how this content affects
227  *  its playlist.
228  */
229 string
230 Content::identifier () const
231 {
232         stringstream s;
233         
234         s << Content::digest()
235           << "_" << position()
236           << "_" << trim_start()
237           << "_" << trim_end();
238
239         return s.str ();
240 }
241
242 bool
243 Content::paths_valid () const
244 {
245         for (vector<boost::filesystem::path>::const_iterator i = _paths.begin(); i != _paths.end(); ++i) {
246                 if (!boost::filesystem::exists (*i)) {
247                         return false;
248                 }
249         }
250
251         return true;
252 }
253
254 void
255 Content::set_path (boost::filesystem::path path)
256 {
257         _paths.clear ();
258         _paths.push_back (path);
259         signal_changed (ContentProperty::PATH);
260 }
261
262 string
263 Content::path_summary () const
264 {
265         /* XXX: should handle multiple paths more gracefully */
266
267         assert (number_of_paths ());
268
269         string s = path(0).filename().string ();
270         if (number_of_paths() > 1) {
271                 s += " ...";
272         }
273
274         return s;
275 }