Merge still/moving image classes.
[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 std::cout;
32 using boost::shared_ptr;
33 using boost::lexical_cast;
34
35 int const ContentProperty::PATH = 400;
36 int const ContentProperty::POSITION = 401;
37 int const ContentProperty::LENGTH = 402;
38 int const ContentProperty::TRIM_START = 403;
39 int const ContentProperty::TRIM_END = 404;
40
41 Content::Content (shared_ptr<const Film> f)
42         : _film (f)
43         , _position (0)
44         , _trim_start (0)
45         , _trim_end (0)
46         , _change_signals_frequent (false)
47 {
48
49 }
50
51 Content::Content (shared_ptr<const Film> f, Time p)
52         : _film (f)
53         , _position (p)
54         , _trim_start (0)
55         , _trim_end (0)
56         , _change_signals_frequent (false)
57 {
58
59 }
60
61 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
62         : _film (f)
63         , _position (0)
64         , _trim_start (0)
65         , _trim_end (0)
66         , _change_signals_frequent (false)
67 {
68         _paths.push_back (p);
69 }
70
71 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
72         : _film (f)
73         , _change_signals_frequent (false)
74 {
75         _paths.push_back (node->string_child ("Path"));
76         _digest = node->string_child ("Digest");
77         _position = node->number_child<Time> ("Position");
78         _trim_start = node->number_child<Time> ("TrimStart");
79         _trim_end = node->number_child<Time> ("TrimEnd");
80 }
81
82 void
83 Content::as_xml (xmlpp::Node* node) const
84 {
85         boost::mutex::scoped_lock lm (_mutex);
86         
87         node->add_child("Path")->add_child_text (_paths.front().string());
88         node->add_child("Digest")->add_child_text (_digest);
89         node->add_child("Position")->add_child_text (lexical_cast<string> (_position));
90         node->add_child("TrimStart")->add_child_text (lexical_cast<string> (_trim_start));
91         node->add_child("TrimEnd")->add_child_text (lexical_cast<string> (_trim_end));
92 }
93
94 void
95 Content::examine (shared_ptr<Job> job)
96 {
97         boost::mutex::scoped_lock lm (_mutex);
98         boost::filesystem::path p = _paths.front ();
99         lm.unlock ();
100         
101         string d;
102         if (boost::filesystem::is_regular_file (p)) {
103                 d = md5_digest (p);
104         } else {
105                 d = md5_digest_directory (p, job);
106         }
107
108         lm.lock ();
109         _digest = d;
110 }
111
112 void
113 Content::signal_changed (int p)
114 {
115         if (ui_signaller) {
116                 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
117         }
118 }
119
120 void
121 Content::set_position (Time p)
122 {
123         {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 _position = p;
126         }
127
128         signal_changed (ContentProperty::POSITION);
129 }
130
131 void
132 Content::set_trim_start (Time t)
133 {
134         {
135                 boost::mutex::scoped_lock lm (_mutex);
136                 _trim_start = t;
137         }
138
139         signal_changed (ContentProperty::TRIM_START);
140 }
141
142 void
143 Content::set_trim_end (Time t)
144 {
145         {
146                 boost::mutex::scoped_lock lm (_mutex);
147                 _trim_end = t;
148         }
149
150         signal_changed (ContentProperty::TRIM_END);
151 }
152
153
154 shared_ptr<Content>
155 Content::clone () const
156 {
157         shared_ptr<const Film> film = _film.lock ();
158         if (!film) {
159                 return shared_ptr<Content> ();
160         }
161         
162         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
163         xmlpp::Document doc;
164         xmlpp::Node* node = doc.create_root_node ("Content");
165         as_xml (node);
166         return content_factory (film, cxml::NodePtr(new cxml::Node (node)));
167 }
168
169 string
170 Content::technical_summary () const
171 {
172         return String::compose ("%1 %2 %3", path(), digest(), position());
173 }
174
175 Time
176 Content::length_after_trim () const
177 {
178         return full_length() - trim_start() - trim_end();
179 }
180
181 /** @param t A time relative to the start of this content (not the position).
182  *  @return true if this time is trimmed by our trim settings.
183  */
184 bool
185 Content::trimmed (Time t) const
186 {
187         return (t < trim_start() || t > (full_length() - trim_end ()));
188 }
189
190 /** @return string which includes everything about how this content affects
191  *  its playlist.
192  */
193 string
194 Content::identifier () const
195 {
196         stringstream s;
197         
198         s << Content::digest()
199           << "_" << position()
200           << "_" << trim_start()
201           << "_" << trim_end();
202
203         return s.str ();
204 }
205
206 bool
207 Content::path_valid () const
208 {
209         return boost::filesystem::exists (_paths.front ());
210 }
211
212 void
213 Content::set_path (boost::filesystem::path path)
214 {
215         _paths.clear ();
216         _paths.push_back (path);
217         signal_changed (ContentProperty::PATH);
218 }
219
220