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