Merge remote-tracking branch 'origin/main' into v2.17.x
[dcpomatic.git] / src / lib / dkdm_recipient.cc
1 /*
2     Copyright (C) 2020-2021 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
22 #include "config.h"
23 #include "dkdm_recipient.h"
24 #include "film.h"
25 #include "kdm_with_metadata.h"
26 #include <dcp/raw_convert.h>
27
28
29 using std::make_shared;
30 using std::shared_ptr;
31 using std::string;
32 using std::vector;
33 using dcp::raw_convert;
34
35
36 DKDMRecipient::DKDMRecipient (cxml::ConstNodePtr node)
37         : KDMRecipient (node)
38 {
39         for (auto i: node->node_children("Email")) {
40                 emails.push_back (i->content());
41         }
42 }
43
44
45 void
46 DKDMRecipient::as_xml (xmlpp::Element* node) const
47 {
48         KDMRecipient::as_xml (node);
49
50         for (auto i: emails) {
51                 cxml::add_text_child(node, "Email", i);
52         }
53 }
54
55
56 KDMWithMetadataPtr
57 kdm_for_dkdm_recipient (
58         shared_ptr<const Film> film,
59         boost::filesystem::path cpl,
60         shared_ptr<DKDMRecipient> recipient,
61         dcp::LocalTime valid_from,
62         dcp::LocalTime valid_to
63         )
64 {
65         if (!recipient->recipient) {
66                 return {};
67         }
68
69         auto signer = Config::instance()->signer_chain();
70         if (!signer->valid()) {
71                 throw InvalidSignerError();
72         }
73
74         auto const decrypted_kdm = film->make_kdm(cpl, valid_from, valid_to);
75         auto const kdm = decrypted_kdm.encrypt(signer, recipient->recipient.get(), {}, dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, 0);
76
77         dcp::NameFormat::Map name_values;
78         name_values['f'] = kdm.content_title_text();
79         name_values['b'] = valid_from.date() + " " + valid_from.time_of_day(true, false);
80         name_values['e'] = valid_to.date() + " " + valid_to.time_of_day(true, false);
81         name_values['i'] = kdm.cpl_id();
82
83         return make_shared<KDMWithMetadata>(name_values, nullptr, recipient->emails, kdm);
84 }
85