Move TrustedDevice to its own source files.
[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
81 KDMWithMetadataPtr
82 kdm_for_screen (
83         shared_ptr<const Film> film,
84         boost::filesystem::path cpl,
85         shared_ptr<const dcpomatic::Screen> screen,
86         boost::posix_time::ptime valid_from,
87         boost::posix_time::ptime valid_to,
88         dcp::Formulation formulation,
89         bool disable_forensic_marking_picture,
90         optional<int> disable_forensic_marking_audio
91         )
92 {
93         if (!screen->recipient) {
94                 return KDMWithMetadataPtr();
95         }
96
97         shared_ptr<const Cinema> cinema = screen->cinema;
98         dcp::LocalTime const begin(valid_from, cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
99         dcp::LocalTime const end  (valid_to,   cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
100
101         dcp::EncryptedKDM const kdm = film->make_kdm (
102                         screen->recipient.get(),
103                         screen->trusted_device_thumbprints(),
104                         cpl,
105                         begin,
106                         end,
107                         formulation,
108                         disable_forensic_marking_picture,
109                         disable_forensic_marking_audio
110                         );
111
112         dcp::NameFormat::Map name_values;
113         if (cinema) {
114                 name_values['c'] = cinema->name;
115         }
116         name_values['s'] = screen->name;
117         name_values['f'] = film->name();
118         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
119         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
120         name_values['i'] = kdm.cpl_id();
121
122         return KDMWithMetadataPtr(new DCPKDMWithMetadata(name_values, cinema, kdm));
123 }
124