dea513ab07f3452bbc2dad2879fce35fde4528e6
[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 std::list;
33 using std::shared_ptr;
34 using boost::optional;
35 using namespace dcpomatic;
36
37 Screen::Screen (cxml::ConstNodePtr node)
38         : KDMRecipient (node)
39 {
40         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children ("TrustedDevice")) {
41                 if (boost::algorithm::starts_with(i->content(), "-----BEGIN CERTIFICATE-----")) {
42                         trusted_devices.push_back (TrustedDevice(dcp::Certificate(i->content())));
43                 } else {
44                         trusted_devices.push_back (TrustedDevice(i->content()));
45                 }
46         }
47 }
48
49 void
50 Screen::as_xml (xmlpp::Element* parent) const
51 {
52         KDMRecipient::as_xml (parent);
53         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
54                 parent->add_child("TrustedDevice")->add_child_text(i.as_string());
55         }
56 }
57
58 vector<string>
59 Screen::trusted_device_thumbprints () const
60 {
61         vector<string> t;
62         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
63                 t.push_back (i.thumbprint());
64         }
65         return t;
66 }
67
68
69 KDMWithMetadataPtr
70 kdm_for_screen (
71         shared_ptr<const Film> film,
72         boost::filesystem::path cpl,
73         shared_ptr<const dcpomatic::Screen> screen,
74         boost::posix_time::ptime valid_from,
75         boost::posix_time::ptime valid_to,
76         dcp::Formulation formulation,
77         bool disable_forensic_marking_picture,
78         optional<int> disable_forensic_marking_audio
79         )
80 {
81         if (!screen->recipient) {
82                 return KDMWithMetadataPtr();
83         }
84
85         shared_ptr<const Cinema> cinema = screen->cinema;
86         dcp::LocalTime const begin(valid_from, cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
87         dcp::LocalTime const end  (valid_to,   cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0);
88
89         dcp::EncryptedKDM const kdm = film->make_kdm (
90                         screen->recipient.get(),
91                         screen->trusted_device_thumbprints(),
92                         cpl,
93                         begin,
94                         end,
95                         formulation,
96                         disable_forensic_marking_picture,
97                         disable_forensic_marking_audio
98                         );
99
100         dcp::NameFormat::Map name_values;
101         if (cinema) {
102                 name_values['c'] = cinema->name;
103         }
104         name_values['s'] = screen->name;
105         name_values['f'] = film->name();
106         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
107         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
108         name_values['i'] = kdm.cpl_id();
109
110         return KDMWithMetadataPtr(new KDMWithMetadata(name_values, cinema.get(), cinema ? cinema->emails : list<string>(), kdm));
111 }
112