Don't store _directory in DCPContent, work it out from the paths instead.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014 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 "dcp_content.h"
21 #include "dcp_examiner.h"
22 #include "job.h"
23 #include "film.h"
24 #include "config.h"
25 #include "compose.hpp"
26 #include <dcp/dcp.h>
27 #include <dcp/exceptions.h>
28 #include <iterator>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::cout;
34 using std::distance;
35 using boost::shared_ptr;
36 using boost::optional;
37
38 int const DCPContentProperty::CAN_BE_PLAYED = 600;
39
40 DCPContent::DCPContent (shared_ptr<const Film> f, boost::filesystem::path p)
41         : Content (f)
42         , VideoContent (f)
43         , SingleStreamAudioContent (f)
44         , SubtitleContent (f)
45         , _has_subtitles (false)
46         , _encrypted (false)
47         , _kdm_valid (false)
48 {
49         read_directory (p);
50 }
51
52 DCPContent::DCPContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
53         : Content (f, node)
54         , VideoContent (f, node, version)
55         , SingleStreamAudioContent (f, node, version)
56         , SubtitleContent (f, node, version)
57 {
58         _name = node->string_child ("Name");
59         _has_subtitles = node->bool_child ("HasSubtitles");
60         _encrypted = node->bool_child ("Encrypted");
61         if (node->optional_node_child ("KDM")) {
62                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
63         }
64         _kdm_valid = node->bool_child ("KDMValid");
65 }
66
67 void
68 DCPContent::read_directory (boost::filesystem::path p)
69 {
70         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
71                 if (boost::filesystem::is_regular_file (i->path ())) {
72                         _paths.push_back (i->path ());
73                 } else if (boost::filesystem::is_directory (i->path ())) {
74                         read_directory (i->path ());
75                 }
76         }
77 }
78
79 void
80 DCPContent::examine (shared_ptr<Job> job)
81 {
82         bool const could_be_played = can_be_played ();
83                 
84         job->set_progress_unknown ();
85         Content::examine (job);
86         
87         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
88         take_from_video_examiner (examiner);
89         take_from_audio_examiner (examiner);
90
91         boost::mutex::scoped_lock lm (_mutex);
92         _name = examiner->name ();
93         _has_subtitles = examiner->has_subtitles ();
94         _encrypted = examiner->encrypted ();
95         _kdm_valid = examiner->kdm_valid ();
96
97         if (could_be_played != can_be_played ()) {
98                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
99         }
100 }
101
102 string
103 DCPContent::summary () const
104 {
105         boost::mutex::scoped_lock lm (_mutex);
106         return String::compose (_("%1 [DCP]"), _name);
107 }
108
109 string
110 DCPContent::technical_summary () const
111 {
112         return Content::technical_summary() + " - "
113                 + VideoContent::technical_summary() + " - "
114                 + AudioContent::technical_summary() + " - ";
115 }
116
117 void
118 DCPContent::as_xml (xmlpp::Node* node) const
119 {
120         node->add_child("Type")->add_child_text ("DCP");
121
122         Content::as_xml (node);
123         VideoContent::as_xml (node);
124         SingleStreamAudioContent::as_xml (node);
125         SubtitleContent::as_xml (node);
126
127         boost::mutex::scoped_lock lm (_mutex);
128         node->add_child("Name")->add_child_text (_name);
129         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
130         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
131         if (_kdm) {
132                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
133         }
134         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
135 }
136
137 DCPTime
138 DCPContent::full_length () const
139 {
140         shared_ptr<const Film> film = _film.lock ();
141         DCPOMATIC_ASSERT (film);
142         return DCPTime (video_length (), FrameRateChange (video_frame_rate (), film->video_frame_rate ()));
143 }
144
145 string
146 DCPContent::identifier () const
147 {
148         return SubtitleContent::identifier ();
149 }
150
151 void
152 DCPContent::add_kdm (dcp::EncryptedKDM k)
153 {
154         _kdm = k;
155 }
156
157 bool
158 DCPContent::can_be_played () const
159 {
160         return !_encrypted || _kdm_valid;
161 }
162
163 boost::filesystem::path
164 DCPContent::directory () const
165 {
166         optional<size_t> smallest;
167         boost::filesystem::path dir;
168         for (size_t i = 0; i < number_of_paths(); ++i) {
169                 boost::filesystem::path const p = path (i).parent_path ();
170                 size_t const d = distance (p.begin(), p.end());
171                 if (!smallest || d < smallest.get ()) {
172                         dir = p;
173                 }
174         }
175
176         return dir;
177 }