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