In-line run of subs_in_out so that it gets the environment more easily.
[libdcp.git] / src / pkl.cc
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/pkl.cc
36  *  @brief PKL class
37  */
38
39
40 #include "dcp_assert.h"
41 #include "exceptions.h"
42 #include "filesystem.h"
43 #include "pkl.h"
44 #include "raw_convert.h"
45 #include "util.h"
46 #include "warnings.h"
47 LIBDCP_DISABLE_WARNINGS
48 #include <libxml++/libxml++.h>
49 LIBDCP_ENABLE_WARNINGS
50 #include <iostream>
51
52
53 using std::string;
54 using std::shared_ptr;
55 using std::make_shared;
56 using boost::optional;
57 using namespace dcp;
58
59
60 static string const pkl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
61 static string const pkl_smpte_ns   = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
62
63
64 PKL::PKL (boost::filesystem::path file)
65         : _file (file)
66 {
67         cxml::Document pkl ("PackingList");
68         pkl.read_file(dcp::filesystem::fix_long_path(file));
69
70         if (pkl.namespace_uri() == pkl_interop_ns) {
71                 _standard = Standard::INTEROP;
72         } else if (pkl.namespace_uri() == pkl_smpte_ns) {
73                 _standard = Standard::SMPTE;
74         } else {
75                 boost::throw_exception(XMLError("Unrecognised packing list namespace " + pkl.namespace_uri()));
76         }
77
78         _id = remove_urn_uuid (pkl.string_child ("Id"));
79         _annotation_text = pkl.optional_string_child ("AnnotationText");
80         _issue_date = pkl.string_child ("IssueDate");
81         _issuer = pkl.string_child ("Issuer");
82         _creator = pkl.string_child ("Creator");
83
84         for (auto i: pkl.node_child("AssetList")->node_children("Asset")) {
85                 _assets.push_back(make_shared<Asset>(i));
86         }
87 }
88
89
90 void
91 PKL::add_asset(std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type, std::string original_filename)
92 {
93         _assets.push_back(make_shared<Asset>(id, annotation_text, hash, size, type, original_filename));
94 }
95
96
97 void
98 PKL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
99 {
100         xmlpp::Document doc;
101         xmlpp::Element* pkl;
102         if (_standard == Standard::INTEROP) {
103                 pkl = doc.create_root_node("PackingList", pkl_interop_ns);
104         } else {
105                 pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
106         }
107
108         pkl->add_child("Id")->add_child_text ("urn:uuid:" + _id);
109         if (_annotation_text) {
110                 pkl->add_child("AnnotationText")->add_child_text (*_annotation_text);
111         }
112         pkl->add_child("IssueDate")->add_child_text (_issue_date);
113         pkl->add_child("Issuer")->add_child_text (_issuer);
114         pkl->add_child("Creator")->add_child_text (_creator);
115
116         auto asset_list = pkl->add_child("AssetList");
117         for (auto i: _assets) {
118                 auto asset = asset_list->add_child("Asset");
119                 asset->add_child("Id")->add_child_text ("urn:uuid:" + i->id());
120                 if (i->annotation_text()) {
121                         asset->add_child("AnnotationText")->add_child_text (*i->annotation_text());
122                 }
123                 asset->add_child("Hash")->add_child_text (i->hash());
124                 asset->add_child("Size")->add_child_text (raw_convert<string>(i->size()));
125                 asset->add_child("Type")->add_child_text (i->type());
126                 if (auto filename = i->original_filename()) {
127                         asset->add_child("OriginalFileName")->add_child_text(*filename);
128                 }
129         }
130
131         indent (pkl, 0);
132
133         if (signer) {
134                 signer->sign (pkl, _standard);
135         }
136
137         doc.write_to_file_formatted(dcp::filesystem::fix_long_path(file).string(), "UTF-8");
138         _file = file;
139 }
140
141
142 optional<string>
143 PKL::hash (string id) const
144 {
145         for (auto i: _assets) {
146                 if (i->id() == id) {
147                         return i->hash();
148                 }
149         }
150
151         return {};
152 }
153
154
155 optional<string>
156 PKL::type (string id) const
157 {
158         for (auto i: _assets) {
159                 if (i->id() == id) {
160                         return i->type();
161                 }
162         }
163
164         return {};
165 }
166
167
168 void
169 PKL::clear_assets()
170 {
171         _assets.clear();
172 }