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