Various improvements to the content properties dialogue (including #791).
[dcpomatic.git] / src / lib / content.h
1 /*
2     Copyright (C) 2013-2015 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 /** @file  src/lib/content.h
21  *  @brief Content class.
22  */
23
24 #ifndef DCPOMATIC_CONTENT_H
25 #define DCPOMATIC_CONTENT_H
26
27 #include "types.h"
28 #include "signaller.h"
29 #include "dcpomatic_time.h"
30 #include "raw_convert.h"
31 #include <libcxml/cxml.h>
32 #include <boost/filesystem.hpp>
33 #include <boost/signals2.hpp>
34 #include <boost/thread/mutex.hpp>
35 #include <boost/enable_shared_from_this.hpp>
36
37 namespace xmlpp {
38         class Node;
39 }
40
41 namespace cxml {
42         class Node;
43 }
44
45 class Job;
46 class Film;
47
48 class ContentProperty
49 {
50 public:
51         static int const PATH;
52         static int const POSITION;
53         static int const LENGTH;
54         static int const TRIM_START;
55         static int const TRIM_END;
56 };
57
58 /** @class Content
59  *  @brief A piece of content represented by one or more files on disk.
60  */
61 class Content : public boost::enable_shared_from_this<Content>, public Signaller, public boost::noncopyable
62 {
63 public:
64         Content (boost::shared_ptr<const Film>);
65         Content (boost::shared_ptr<const Film>, DCPTime);
66         Content (boost::shared_ptr<const Film>, boost::filesystem::path);
67         Content (boost::shared_ptr<const Film>, cxml::ConstNodePtr);
68         Content (boost::shared_ptr<const Film>, std::vector<boost::shared_ptr<Content> >);
69         virtual ~Content () {}
70
71         /** Examine the content to establish digest, frame rates and any other
72          *  useful metadata.
73          *  @param job Job to use to report progress, or 0.
74          */
75         virtual void examine (boost::shared_ptr<Job> job);
76
77         /** @return Quick one-line summary of the content, as will be presented in the
78          *  film editor.
79          */
80         virtual std::string summary () const = 0;
81
82         /** @return Technical details of this content; these are written to logs to
83          *  help with debugging.
84          */
85         virtual std::string technical_summary () const;
86
87         virtual void as_xml (xmlpp::Node *) const;
88         virtual DCPTime full_length () const = 0;
89         virtual std::string identifier () const;
90         /** @return points at which to split this content when
91          *  REELTYPE_BY_VIDEO_CONTENT is in use.
92          */
93         virtual std::list<DCPTime> reel_split_points () const;
94
95         boost::shared_ptr<Content> clone () const;
96
97         void set_path (boost::filesystem::path);
98
99         std::string path_summary () const;
100
101         std::vector<boost::filesystem::path> paths () const {
102                 boost::mutex::scoped_lock lm (_mutex);
103                 return _paths;
104         }
105
106         size_t number_of_paths () const {
107                 boost::mutex::scoped_lock lm (_mutex);
108                 return _paths.size ();
109         }
110
111         boost::filesystem::path path (size_t i) const {
112                 boost::mutex::scoped_lock lm (_mutex);
113                 return _paths[i];
114         }
115
116         bool paths_valid () const;
117
118         /** @return Digest of the content's file(s).  Note: this is
119          *  not a complete MD5-or-whatever hash, but a sort of poor
120          *  man's version (see comments in ::examine).
121          */
122         std::string digest () const {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 return _digest;
125         }
126
127         void set_position (DCPTime);
128
129         /** DCPTime that this content starts; i.e. the time that the first
130          *  bit of the content (trimmed or not) will happen.
131          */
132         DCPTime position () const {
133                 boost::mutex::scoped_lock lm (_mutex);
134                 return _position;
135         }
136
137         void set_trim_start (ContentTime);
138
139         ContentTime trim_start () const {
140                 boost::mutex::scoped_lock lm (_mutex);
141                 return _trim_start;
142         }
143
144         void set_trim_end (ContentTime);
145
146         ContentTime trim_end () const {
147                 boost::mutex::scoped_lock lm (_mutex);
148                 return _trim_end;
149         }
150
151         /** @return Time immediately after the last thing in this content */
152         DCPTime end () const {
153                 return position() + length_after_trim();
154         }
155
156         DCPTime length_after_trim () const;
157
158         void set_change_signals_frequent (bool f) {
159                 _change_signals_frequent = f;
160         }
161
162         boost::shared_ptr<const Film> film () const;
163
164         class UserProperty
165         {
166         public:
167                 template <class T>
168                 UserProperty (std::string category_, std::string key_, T value_, std::string unit_ = "")
169                         : category (category_)
170                         , key (key_)
171                         , value (raw_convert<std::string> (value_))
172                         , unit (unit_)
173                 {}
174
175                 std::string category;
176                 std::string key;
177                 std::string value;
178                 std::string unit;
179         };
180
181         std::list<UserProperty> user_properties () const;
182
183         boost::signals2::signal<void (boost::weak_ptr<Content>, int, bool)> Changed;
184
185 protected:
186         void signal_changed (int);
187
188         virtual void add_properties (std::list<UserProperty> &) const {}
189
190         boost::weak_ptr<const Film> _film;
191
192         /** _mutex which should be used to protect accesses, as examine
193          *  jobs can update content state in threads other than the main one.
194          */
195         mutable boost::mutex _mutex;
196
197         /** Paths of our data files */
198         std::vector<boost::filesystem::path> _paths;
199
200 private:
201         std::string _digest;
202         DCPTime _position;
203         ContentTime _trim_start;
204         ContentTime _trim_end;
205         bool _change_signals_frequent;
206 };
207
208 #endif