6323708101b6b0ff9f20748d18fc651593c68ba1
[dcpomatic.git] / src / lib / screen.cc
1 /*
2     Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "screen.h"
22 #include "kdm_with_metadata.h"
23 #include "film.h"
24 #include "cinema.h"
25 #include <libxml++/libxml++.h>
26 #include <boost/foreach.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <boost/date_time/posix_time/posix_time.hpp>
29
30 using std::string;
31 using std::vector;
32 using boost::shared_ptr;
33 using boost::optional;
34 using namespace dcpomatic;
35
36 Screen::Screen (cxml::ConstNodePtr node)
37         : name (node->string_child("Name"))
38         , notes (node->optional_string_child("Notes").get_value_or (""))
39 {
40         if (node->optional_string_child ("Certificate")) {
41                 recipient = dcp::Certificate (node->string_child ("Certificate"));
42         } else if (node->optional_string_child ("Recipient")) {
43                 recipient = dcp::Certificate (node->string_child ("Recipient"));
44         }
45
46         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children ("TrustedDevice")) {
47                 if (boost::algorithm::starts_with(i->content(), "-----BEGIN CERTIFICATE-----")) {
48                         trusted_devices.push_back (TrustedDevice(dcp::Certificate(i->content())));
49                 } else {
50                         trusted_devices.push_back (TrustedDevice(i->content()));
51                 }
52         }
53 }
54
55 void
56 Screen::as_xml (xmlpp::Element* parent) const
57 {
58         parent->add_child("Name")->add_child_text (name);
59         if (recipient) {
60                 parent->add_child("Recipient")->add_child_text (recipient->certificate (true));
61         }
62
63         parent->add_child("Notes")->add_child_text (notes);
64
65         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
66                 parent->add_child("TrustedDevice")->add_child_text(i.as_string());
67         }
68 }
69
70 vector<string>
71 Screen::trusted_device_thumbprints () const
72 {
73         vector<string> t;
74         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
75                 t.push_back (i.thumbprint());
76         }
77         return t;
78 }
79
80 TrustedDevice::TrustedDevice (string thumbprint)
81         : _thumbprint (thumbprint)
82 {
83
84 }
85
86 TrustedDevice::TrustedDevice (dcp::Certificate certificate)
87         : _certificate (certificate)
88 {
89
90 }
91
92 string
93 TrustedDevice::as_string () const
94 {
95         if (_certificate) {
96                 return _certificate->certificate(true);
97         }
98
99         return *_thumbprint;
100 }
101
102 string
103 TrustedDevice::thumbprint () const
104 {
105         if (_certificate) {
106                 return _certificate->thumbprint ();
107         }
108
109         return *_thumbprint;
110 }
111
112
113 KDMWithMetadataPtr
114 kdm_for_screen (
115         shared_ptr<const Film> film,
116         boost::filesystem::path cpl,
117         shared_ptr<const dcpomatic::Screen> screen,
118         boost::posix_time::ptime valid_from,
119         boost::posix_time::ptime valid_to,
120         dcp::Formulation formulation,
121         bool disable_forensic_marking_picture,
122         optional<int> disable_forensic_marking_audio
123         )
124 {
125         if (!screen->recipient) {
126                 return KDMWithMetadataPtr();
127         }
128
129         shared_ptr<const Cinema> cinema = screen->cinema;
130         dcp::LocalTime const begin(valid_from, cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
131         dcp::LocalTime const end  (valid_to,   cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
132
133         dcp::EncryptedKDM const kdm = film->make_kdm (
134                         screen->recipient.get(),
135                         screen->trusted_device_thumbprints(),
136                         cpl,
137                         begin,
138                         end,
139                         formulation,
140                         disable_forensic_marking_picture,
141                         disable_forensic_marking_audio
142                         );
143
144         dcp::NameFormat::Map name_values;
145         if (cinema) {
146                 name_values['c'] = cinema->name;
147         }
148         name_values['s'] = screen->name;
149         name_values['f'] = film->name();
150         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
151         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
152         name_values['i'] = kdm.cpl_id();
153
154         return KDMWithMetadataPtr(new DCPKDMWithMetadata(name_values, cinema, kdm));
155 }
156