Another macOS std::list boost::thread SNAFU.
[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 <libxml++/libxml++.h>
25 #include <boost/foreach.hpp>
26
27 using std::string;
28 using std::list;
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
31
32 shared_ptr<DKDMBase>
33 DKDMBase::read (cxml::ConstNodePtr node)
34 {
35         if (node->name() == "DKDM") {
36                 return shared_ptr<DKDM> (new DKDM (dcp::EncryptedKDM (node->content ())));
37 #ifdef DCPOMATIC_VARIANT_SWAROOP
38         } else if (node->name() == "ECinemaDKDM") {
39                 return shared_ptr<ECinemaDKDM> (new ECinemaDKDM(EncryptedECinemaKDM(node->content())));
40 #endif
41         } else if (node->name() == "DKDMGroup") {
42                 shared_ptr<DKDMGroup> group (new DKDMGroup (node->string_attribute ("Name")));
43                 BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children()) {
44                         shared_ptr<DKDMBase> c = read (i);
45                         if (c) {
46                                 group->add (c);
47                         }
48                 }
49                 return group;
50         }
51
52         return shared_ptr<DKDMBase> ();
53 }
54
55 string
56 DKDM::name () const
57 {
58         return String::compose ("%1 (%2)", _dkdm.content_title_text(), _dkdm.cpl_id());
59 }
60
61 void
62 DKDM::as_xml (xmlpp::Element* node) const
63 {
64         node->add_child("DKDM")->add_child_text (_dkdm.as_xml ());
65 }
66
67 #ifdef DCPOMATIC_VARIANT_SWAROOP
68 string
69 ECinemaDKDM::name () const
70 {
71         return String::compose ("%1 (%2)", _dkdm.name(), _dkdm.id());
72 }
73
74 void
75 ECinemaDKDM::as_xml (xmlpp::Element* node) const
76 {
77         node->add_child("ECinemaDKDM")->add_child_text (_dkdm.as_xml());
78 }
79 #endif
80
81 void
82 DKDMGroup::as_xml (xmlpp::Element* node) const
83 {
84         xmlpp::Element* f = node->add_child("DKDMGroup");
85         f->set_attribute ("Name", _name);
86         BOOST_FOREACH (shared_ptr<DKDMBase> i, _children) {
87                 i->as_xml (f);
88         }
89 }
90
91 void
92 DKDMGroup::add (shared_ptr<DKDMBase> child, shared_ptr<DKDM> previous)
93 {
94         DCPOMATIC_ASSERT (child);
95         if (previous) {
96                 list<shared_ptr<DKDMBase> >::iterator i = find (_children.begin(), _children.end(), previous);
97                 if (i != _children.end ()) {
98                         ++i;
99                 }
100                 _children.insert (i, child);
101         } else {
102                 _children.push_back (child);
103         }
104         child->set_parent (dynamic_pointer_cast<DKDMGroup> (shared_from_this ()));
105 }
106
107 void
108 DKDMGroup::remove (shared_ptr<DKDMBase> child)
109 {
110         for (list<shared_ptr<DKDMBase> >::iterator i = _children.begin(); i != _children.end(); ++i) {
111                 if (*i == child) {
112                         _children.erase (i);
113                         child->set_parent (shared_ptr<DKDMGroup> ());
114                         return;
115                 }
116                 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup> (*i);
117                 if (g) {
118                         g->remove (child);
119                 }
120         }
121 }