Missed update to private test repo version.
[dcpomatic.git] / src / lib / screen.cc
1 /*
2     Copyright (C) 2013-2016 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 "screen.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/foreach.hpp>
24 #include <boost/algorithm/string.hpp>
25
26 using std::string;
27 using std::vector;
28
29 Screen::Screen (cxml::ConstNodePtr node)
30         : name (node->string_child("Name"))
31         , notes (node->optional_string_child("Notes").get_value_or (""))
32 {
33         if (node->optional_string_child ("Certificate")) {
34                 recipient = dcp::Certificate (node->string_child ("Certificate"));
35         } else if (node->optional_string_child ("Recipient")) {
36                 recipient = dcp::Certificate (node->string_child ("Recipient"));
37         }
38
39         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children ("TrustedDevice")) {
40                 if (boost::algorithm::starts_with(i->content(), "-----BEGIN CERTIFICATE-----")) {
41                         trusted_devices.push_back (TrustedDevice(dcp::Certificate(i->content())));
42                 } else {
43                         trusted_devices.push_back (TrustedDevice(i->content()));
44                 }
45         }
46 }
47
48 void
49 Screen::as_xml (xmlpp::Element* parent) const
50 {
51         parent->add_child("Name")->add_child_text (name);
52         if (recipient) {
53                 parent->add_child("Recipient")->add_child_text (recipient->certificate (true));
54         }
55
56         parent->add_child("Notes")->add_child_text (notes);
57
58         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
59                 parent->add_child("TrustedDevice")->add_child_text(i.as_string());
60         }
61 }
62
63 vector<string>
64 Screen::trusted_device_thumbprints () const
65 {
66         vector<string> t;
67         BOOST_FOREACH (TrustedDevice i, trusted_devices) {
68                 t.push_back (i.thumbprint());
69         }
70         return t;
71 }
72
73 TrustedDevice::TrustedDevice (string thumbprint)
74         : _thumbprint (thumbprint)
75 {
76
77 }
78
79 TrustedDevice::TrustedDevice (dcp::Certificate certificate)
80         : _certificate (certificate)
81 {
82
83 }
84
85 string
86 TrustedDevice::as_string () const
87 {
88         if (_certificate) {
89                 return _certificate->certificate(true);
90         }
91
92         return *_thumbprint;
93 }
94
95 string
96 TrustedDevice::thumbprint () const
97 {
98         if (_certificate) {
99                 return _certificate->thumbprint ();
100         }
101
102         return *_thumbprint;
103 }