C++11 tidying.
[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 "warnings.h"
26 DCPOMATIC_DISABLE_WARNINGS
27 #include <libxml++/libxml++.h>
28 DCPOMATIC_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 group = make_shared<DKDMGroup>(node->string_attribute("Name"));
45                 for (auto i: node->node_children()) {
46                         if (auto c = read(i)) {
47                                 group->add (c);
48                         }
49                 }
50                 return group;
51         }
52
53         return {};
54 }
55
56
57 string
58 DKDM::name () const
59 {
60         return String::compose ("%1 (%2)", _dkdm.content_title_text(), _dkdm.cpl_id());
61 }
62
63
64 void
65 DKDM::as_xml (xmlpp::Element* node) const
66 {
67         node->add_child("DKDM")->add_child_text (_dkdm.as_xml ());
68 }
69
70
71 void
72 DKDMGroup::as_xml (xmlpp::Element* node) const
73 {
74         auto f = node->add_child("DKDMGroup");
75         f->set_attribute ("Name", _name);
76         for (auto i: _children) {
77                 i->as_xml (f);
78         }
79 }
80
81
82 void
83 DKDMGroup::add (shared_ptr<DKDMBase> child, shared_ptr<DKDM> previous)
84 {
85         DCPOMATIC_ASSERT (child);
86         if (previous) {
87                 auto i = find (_children.begin(), _children.end(), previous);
88                 if (i != _children.end()) {
89                         ++i;
90                 }
91                 _children.insert (i, child);
92         } else {
93                 _children.push_back (child);
94         }
95         child->set_parent (dynamic_pointer_cast<DKDMGroup>(shared_from_this()));
96 }
97
98
99 void
100 DKDMGroup::remove (shared_ptr<DKDMBase> child)
101 {
102         for (auto i = _children.begin(); i != _children.end(); ++i) {
103                 if (*i == child) {
104                         _children.erase (i);
105                         child->set_parent (shared_ptr<DKDMGroup>());
106                         return;
107                 }
108                 auto g = dynamic_pointer_cast<DKDMGroup> (*i);
109                 if (g) {
110                         g->remove (child);
111                 }
112         }
113 }