Tidy up write_xml() API a little.
[libdcp.git] / src / pkl.h
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 #ifndef LIBDCP_PKL_H
41 #define LIBDCP_PKL_H
42
43
44 #include "object.h"
45 #include "types.h"
46 #include "util.h"
47 #include "certificate_chain.h"
48 #include <libcxml/cxml.h>
49 #include <boost/filesystem.hpp>
50
51
52 namespace dcp {
53
54
55 class PKL : public Object
56 {
57 public:
58         PKL (Standard standard, boost::optional<std::string> annotation_text, std::string issue_date, std::string issuer, std::string creator)
59                 : _standard (standard)
60                 , _annotation_text (annotation_text)
61                 , _issue_date (issue_date)
62                 , _issuer (issuer)
63                 , _creator (creator)
64         {}
65
66         explicit PKL (boost::filesystem::path file);
67
68         Standard standard () const {
69                 return _standard;
70         }
71
72         boost::optional<std::string> annotation_text () const {
73                 return _annotation_text;
74         }
75
76         void set_annotation_text(std::string annotation_text) {
77                 _annotation_text = annotation_text;
78         }
79
80         void set_issue_date(std::string issue_date) {
81                 _issue_date = issue_date;
82         }
83
84         void set_issuer(std::string issuer) {
85                 _issuer = issuer;
86         }
87
88         void set_creator(std::string creator) {
89                 _creator = creator;
90         }
91
92         boost::optional<std::string> hash (std::string id) const;
93         boost::optional<std::string> type (std::string id) const;
94
95         void clear_assets();
96         void add_asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type);
97         void write (boost::filesystem::path file, std::shared_ptr<const CertificateChain> signer) const;
98
99         /** @return the most recent disk file used to read or write this PKL, if there is one */
100         boost::optional<boost::filesystem::path> file () const {
101                 return _file;
102         }
103
104         class Asset : public Object
105         {
106         public:
107                 Asset (cxml::ConstNodePtr node)
108                         : Object (remove_urn_uuid(node->string_child("Id")))
109                         , _annotation_text (node->optional_string_child("AnnotationText"))
110                         , _hash (node->string_child("Hash"))
111                         , _size (node->number_child<int64_t>("Size"))
112                         , _type (node->string_child("Type"))
113                 {}
114
115                 Asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
116                         : Object (id)
117                         , _annotation_text (annotation_text)
118                         , _hash (hash)
119                         , _size (size)
120                         , _type (type)
121                 {}
122
123                 boost::optional<std::string> annotation_text () const {
124                         return _annotation_text;
125                 }
126
127                 std::string hash () const {
128                         return _hash;
129                 }
130
131                 int64_t size () const {
132                         return _size;
133                 }
134
135                 std::string type () const {
136                         return _type;
137                 }
138
139         private:
140                 boost::optional<std::string> _annotation_text;
141                 std::string _hash;
142                 int64_t _size = 0;
143                 std::string _type;
144         };
145
146         std::vector<std::shared_ptr<Asset>> asset_list () const {
147                 return _asset_list;
148         }
149
150 private:
151
152         Standard _standard = dcp::Standard::SMPTE;
153         boost::optional<std::string> _annotation_text;
154         std::string _issue_date;
155         std::string _issuer;
156         std::string _creator;
157         std::vector<std::shared_ptr<Asset>> _asset_list;
158         /** The most recent disk file used to read or write this PKL */
159         mutable boost::optional<boost::filesystem::path> _file;
160 };
161
162
163 }
164
165
166 #endif