Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / cinema.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 "cinema.h"
22 #include "screen.h"
23 #include "dcpomatic_assert.h"
24 #include <libcxml/cxml.h>
25 #include <dcp/raw_convert.h>
26 #include <libxml++/libxml++.h>
27 #include <boost/foreach.hpp>
28 #include <iostream>
29
30 using std::list;
31 using std::string;
32 using boost::shared_ptr;
33 using dcp::raw_convert;
34 using dcpomatic::Screen;
35
36 Cinema::Cinema (cxml::ConstNodePtr node)
37         : name (node->string_child ("Name"))
38         , notes (node->optional_string_child("Notes").get_value_or(""))
39 {
40         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("Email")) {
41                 emails.push_back (i->content ());
42         }
43
44         if (node->optional_number_child<int>("UTCOffset")) {
45                 _utc_offset_hour = node->number_child<int>("UTCOffset");
46         } else {
47                 _utc_offset_hour = node->optional_number_child<int>("UTCOffsetHour").get_value_or (0);
48         }
49
50         _utc_offset_minute = node->optional_number_child<int>("UTCOffsetMinute").get_value_or (0);
51 }
52
53 /* This is necessary so that we can use shared_from_this in add_screen (which cannot be done from
54    a constructor)
55 */
56 void
57 Cinema::read_screens (cxml::ConstNodePtr node)
58 {
59         list<cxml::NodePtr> s = node->node_children ("Screen");
60         for (list<cxml::NodePtr>::iterator i = s.begin(); i != s.end(); ++i) {
61                 add_screen (shared_ptr<Screen> (new Screen (*i)));
62         }
63 }
64
65 void
66 Cinema::as_xml (xmlpp::Element* parent) const
67 {
68         parent->add_child("Name")->add_child_text (name);
69
70         BOOST_FOREACH (string i, emails) {
71                 parent->add_child("Email")->add_child_text (i);
72         }
73
74         parent->add_child("Notes")->add_child_text (notes);
75
76         parent->add_child("UTCOffsetHour")->add_child_text (raw_convert<string> (_utc_offset_hour));
77         parent->add_child("UTCOffsetMinute")->add_child_text (raw_convert<string> (_utc_offset_minute));
78
79         BOOST_FOREACH (shared_ptr<Screen> i, _screens) {
80                 i->as_xml (parent->add_child ("Screen"));
81         }
82 }
83
84 void
85 Cinema::add_screen (shared_ptr<Screen> s)
86 {
87         s->cinema = shared_from_this ();
88         _screens.push_back (s);
89 }
90
91 void
92 Cinema::remove_screen (shared_ptr<Screen> s)
93 {
94         _screens.remove (s);
95 }
96
97 void
98 Cinema::set_utc_offset_hour (int h)
99 {
100         DCPOMATIC_ASSERT (h >= -11 && h <= 12);
101         _utc_offset_hour = h;
102 }
103
104 void
105 Cinema::set_utc_offset_minute (int m)
106 {
107         DCPOMATIC_ASSERT (m >= 0 && m <= 59);
108         _utc_offset_minute = m;
109 }