Supporters update.
[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 #include <dcp/utc_offset.h>
28
29
30 using std::make_shared;
31 using std::shared_ptr;
32 using std::string;
33 using std::vector;
34 using dcp::raw_convert;
35
36
37 DKDMRecipient::DKDMRecipient (cxml::ConstNodePtr node)
38         : KDMRecipient (node)
39 {
40         for (auto i: node->node_children("Email")) {
41                 emails.push_back (i->content());
42         }
43
44         utc_offset_hour = node->number_child<int>("UTCOffsetHour");
45         utc_offset_minute = node->number_child<int>("UTCOffsetMinute");
46 }
47
48
49 void
50 DKDMRecipient::as_xml (xmlpp::Element* node) const
51 {
52         KDMRecipient::as_xml (node);
53
54         for (auto i: emails) {
55                 node->add_child("Email")->add_child_text(i);
56         }
57
58         node->add_child("UTCOffsetHour")->add_child_text(raw_convert<string>(utc_offset_hour));
59         node->add_child("UTCOffsetMinute")->add_child_text(raw_convert<string>(utc_offset_minute));
60 }
61
62
63 KDMWithMetadataPtr
64 kdm_for_dkdm_recipient (
65         shared_ptr<const Film> film,
66         boost::filesystem::path cpl,
67         shared_ptr<DKDMRecipient> recipient,
68         boost::posix_time::ptime valid_from,
69         boost::posix_time::ptime valid_to
70         )
71 {
72         if (!recipient->recipient) {
73                 return {};
74         }
75
76         dcp::LocalTime const begin(valid_from, dcp::UTCOffset(recipient->utc_offset_hour, recipient->utc_offset_minute));
77         dcp::LocalTime const end  (valid_to,   dcp::UTCOffset(recipient->utc_offset_hour, recipient->utc_offset_minute));
78
79         auto signer = Config::instance()->signer_chain();
80         if (!signer->valid()) {
81                 throw InvalidSignerError();
82         }
83
84         auto const decrypted_kdm = film->make_kdm(cpl, begin, end);
85         auto const kdm = decrypted_kdm.encrypt(signer, recipient->recipient.get(), {}, dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, 0);
86
87         dcp::NameFormat::Map name_values;
88         name_values['f'] = kdm.content_title_text();
89         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
90         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
91         name_values['i'] = kdm.cpl_id();
92
93         return make_shared<KDMWithMetadata>(name_values, nullptr, recipient->emails, kdm);
94 }
95