Tidying.
[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 "pkl.h"
41 #include "exceptions.h"
42 #include "util.h"
43 #include "raw_convert.h"
44 #include "dcp_assert.h"
45 #include <libxml++/libxml++.h>
46 #include <iostream>
47
48
49 using std::string;
50 using std::shared_ptr;
51 using std::make_shared;
52 using boost::optional;
53 using namespace dcp;
54
55
56 static string const pkl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
57 static string const pkl_smpte_ns   = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
58
59
60 PKL::PKL (boost::filesystem::path file)
61         : _file (file)
62 {
63         cxml::Document pkl ("PackingList");
64         pkl.read_file (file);
65
66         if (pkl.namespace_uri() == pkl_interop_ns) {
67                 _standard = Standard::INTEROP;
68         } else if (pkl.namespace_uri() == pkl_smpte_ns) {
69                 _standard = Standard::SMPTE;
70         } else {
71                 boost::throw_exception (XMLError ("Unrecognised packing list namesapce " + pkl.namespace_uri()));
72         }
73
74         _id = remove_urn_uuid (pkl.string_child ("Id"));
75         _annotation_text = pkl.optional_string_child ("AnnotationText");
76         _issue_date = pkl.string_child ("IssueDate");
77         _issuer = pkl.string_child ("Issuer");
78         _creator = pkl.string_child ("Creator");
79
80         for (auto i: pkl.node_child("AssetList")->node_children("Asset")) {
81                 _asset_list.push_back (make_shared<Asset>(i));
82         }
83 }
84
85
86 void
87 PKL::add_asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
88 {
89         _asset_list.push_back (make_shared<Asset>(id, annotation_text, hash, size, type));
90 }
91
92
93 void
94 PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
95 {
96         xmlpp::Document doc;
97         xmlpp::Element* pkl;
98         if (_standard == Standard::INTEROP) {
99                 pkl = doc.create_root_node("PackingList", pkl_interop_ns);
100         } else {
101                 pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
102         }
103
104         pkl->add_child("Id")->add_child_text ("urn:uuid:" + _id);
105         if (_annotation_text) {
106                 pkl->add_child("AnnotationText")->add_child_text (*_annotation_text);
107         }
108         pkl->add_child("IssueDate")->add_child_text (_issue_date);
109         pkl->add_child("Issuer")->add_child_text (_issuer);
110         pkl->add_child("Creator")->add_child_text (_creator);
111
112         auto asset_list = pkl->add_child("AssetList");
113         for (auto i: _asset_list) {
114                 auto asset = asset_list->add_child("Asset");
115                 asset->add_child("Id")->add_child_text ("urn:uuid:" + i->id());
116                 if (i->annotation_text()) {
117                         asset->add_child("AnnotationText")->add_child_text (*i->annotation_text());
118                 }
119                 asset->add_child("Hash")->add_child_text (i->hash());
120                 asset->add_child("Size")->add_child_text (raw_convert<string>(i->size()));
121                 asset->add_child("Type")->add_child_text (i->type());
122         }
123
124         indent (pkl, 0);
125
126         if (signer) {
127                 signer->sign (pkl, _standard);
128         }
129
130         doc.write_to_file_formatted (file.string(), "UTF-8");
131         _file = file;
132 }
133
134
135 optional<string>
136 PKL::hash (string id) const
137 {
138         for (auto i: _asset_list) {
139                 if (i->id() == id) {
140                         return i->hash();
141                 }
142         }
143
144         return {};
145 }
146
147
148 optional<string>
149 PKL::type (string id) const
150 {
151         for (auto i: _asset_list) {
152                 if (i->id() == id) {
153                         return i->type();
154                 }
155         }
156
157         return {};
158 }