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