Normalise XML attribute names to be camelCase (#2241).
[dcpomatic.git] / src / lib / dkdm_wrapper.cc
1 /*
2     Copyright (C) 2017-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 "compose.hpp"
23 #include "dkdm_wrapper.h"
24 #include "dcpomatic_assert.h"
25 #include <dcp/warnings.h>
26 LIBDCP_DISABLE_WARNINGS
27 #include <libxml++/libxml++.h>
28 LIBDCP_ENABLE_WARNINGS
29
30
31 using std::string;
32 using std::list;
33 using std::shared_ptr;
34 using std::make_shared;
35 using std::dynamic_pointer_cast;
36
37
38 shared_ptr<DKDMBase>
39 DKDMBase::read (cxml::ConstNodePtr node)
40 {
41         if (node->name() == "DKDM") {
42                 return make_shared<DKDM>(dcp::EncryptedKDM(node->content()));
43         } else if (node->name() == "DKDMGroup") {
44                 auto name = node->optional_string_attribute("Name");
45                 if (!name) {
46                         name = node->string_attribute("name");
47                 }
48                 auto group = make_shared<DKDMGroup>(*name);
49                 for (auto i: node->node_children()) {
50                         if (auto c = read(i)) {
51                                 group->add (c);
52                         }
53                 }
54                 return group;
55         }
56
57         return {};
58 }
59
60
61 string
62 DKDM::name () const
63 {
64         return String::compose ("%1 (%2)", _dkdm.content_title_text(), _dkdm.cpl_id());
65 }
66
67
68 void
69 DKDM::as_xml (xmlpp::Element* node) const
70 {
71         node->add_child("DKDM")->add_child_text (_dkdm.as_xml ());
72 }
73
74
75 void
76 DKDMGroup::as_xml (xmlpp::Element* node) const
77 {
78         auto f = node->add_child("DKDMGroup");
79         f->set_attribute("name", _name);
80         for (auto i: _children) {
81                 i->as_xml (f);
82         }
83 }
84
85
86 void
87 DKDMGroup::add (shared_ptr<DKDMBase> child, shared_ptr<DKDM> previous)
88 {
89         DCPOMATIC_ASSERT (child);
90         if (previous) {
91                 auto i = find (_children.begin(), _children.end(), previous);
92                 if (i != _children.end()) {
93                         ++i;
94                 }
95                 _children.insert (i, child);
96         } else {
97                 _children.push_back (child);
98         }
99         child->set_parent (dynamic_pointer_cast<DKDMGroup>(shared_from_this()));
100 }
101
102
103 void
104 DKDMGroup::remove (shared_ptr<DKDMBase> child)
105 {
106         for (auto i = _children.begin(); i != _children.end(); ++i) {
107                 if (*i == child) {
108                         _children.erase (i);
109                         child->set_parent (shared_ptr<DKDMGroup>());
110                         return;
111                 }
112                 auto g = dynamic_pointer_cast<DKDMGroup> (*i);
113                 if (g) {
114                         g->remove (child);
115                 }
116         }
117 }