Support TGA file directories and improve progress reporting when hashing directories...
[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::set;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32
33 int const ContentProperty::POSITION = 400;
34 int const ContentProperty::LENGTH = 401;
35 int const ContentProperty::TRIM_START = 402;
36 int const ContentProperty::TRIM_END = 403;
37
38 Content::Content (shared_ptr<const Film> f, Time p)
39         : _film (f)
40         , _position (p)
41         , _trim_start (0)
42         , _trim_end (0)
43         , _change_signals_frequent (false)
44 {
45
46 }
47
48 Content::Content (shared_ptr<const Film> f, boost::filesystem::path p)
49         : _film (f)
50         , _path (p)
51         , _position (0)
52         , _trim_start (0)
53         , _trim_end (0)
54         , _change_signals_frequent (false)
55 {
56
57 }
58
59 Content::Content (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
60         : _film (f)
61         , _change_signals_frequent (false)
62 {
63         _path = node->string_child ("Path");
64         _digest = node->string_child ("Digest");
65         _position = node->number_child<Time> ("Position");
66         _trim_start = node->number_child<Time> ("TrimStart");
67         _trim_end = node->number_child<Time> ("TrimEnd");
68 }
69
70 void
71 Content::as_xml (xmlpp::Node* node) const
72 {
73         boost::mutex::scoped_lock lm (_mutex);
74         
75         node->add_child("Path")->add_child_text (_path.string());
76         node->add_child("Digest")->add_child_text (_digest);
77         node->add_child("Position")->add_child_text (lexical_cast<string> (_position));
78         node->add_child("TrimStart")->add_child_text (lexical_cast<string> (_trim_start));
79         node->add_child("TrimEnd")->add_child_text (lexical_cast<string> (_trim_end));
80 }
81
82 void
83 Content::examine (shared_ptr<Job> job)
84 {
85         boost::mutex::scoped_lock lm (_mutex);
86         boost::filesystem::path p = _path;
87         lm.unlock ();
88         
89         string d;
90         if (boost::filesystem::is_regular_file (p)) {
91                 d = md5_digest (p);
92         } else {
93                 d = md5_digest_directory (p, job);
94         }
95
96         lm.lock ();
97         _digest = d;
98 }
99
100 void
101 Content::signal_changed (int p)
102 {
103         if (ui_signaller) {
104                 ui_signaller->emit (boost::bind (boost::ref (Changed), shared_from_this (), p, _change_signals_frequent));
105         }
106 }
107
108 void
109 Content::set_position (Time p)
110 {
111         {
112                 boost::mutex::scoped_lock lm (_mutex);
113                 _position = p;
114         }
115
116         signal_changed (ContentProperty::POSITION);
117 }
118
119 void
120 Content::set_trim_start (Time t)
121 {
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 _trim_start = t;
125         }
126
127         signal_changed (ContentProperty::TRIM_START);
128 }
129
130 void
131 Content::set_trim_end (Time t)
132 {
133         {
134                 boost::mutex::scoped_lock lm (_mutex);
135                 _trim_end = t;
136         }
137
138         signal_changed (ContentProperty::TRIM_END);
139 }
140
141
142 shared_ptr<Content>
143 Content::clone () const
144 {
145         shared_ptr<const Film> film = _film.lock ();
146         if (!film) {
147                 return shared_ptr<Content> ();
148         }
149         
150         /* This is a bit naughty, but I can't think of a compelling reason not to do it ... */
151         xmlpp::Document doc;
152         xmlpp::Node* node = doc.create_root_node ("Content");
153         as_xml (node);
154         return content_factory (film, shared_ptr<cxml::Node> (new cxml::Node (node)));
155 }
156
157 string
158 Content::technical_summary () const
159 {
160         return String::compose ("%1 %2 %3", path(), digest(), position());
161 }
162
163 Time
164 Content::length_after_trim () const
165 {
166         return full_length() - trim_start() - trim_end();
167 }
168
169 /** @param t A time relative to the start of this content (not the position).
170  *  @return true if this time is trimmed by our trim settings.
171  */
172 bool
173 Content::trimmed (Time t) const
174 {
175         return (t < trim_start() || t > (full_length() - trim_end ()));
176 }