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