Restore time zone to Cinema and improve UI to use it (#2473).
[dcpomatic.git] / src / lib / cinema.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "cinema.h"
23 #include "screen.h"
24 #include "dcpomatic_assert.h"
25 #include <libcxml/cxml.h>
26 #include <dcp/raw_convert.h>
27 #include <libxml++/libxml++.h>
28
29
30 using std::make_shared;
31 using std::shared_ptr;
32 using std::string;
33 using dcp::raw_convert;
34 using dcpomatic::Screen;
35
36
37 Cinema::Cinema (cxml::ConstNodePtr node)
38         : name (node->string_child ("Name"))
39         , notes (node->optional_string_child("Notes").get_value_or(""))
40 {
41         for (auto i: node->node_children("Email")) {
42                 emails.push_back (i->content ());
43         }
44
45         int hour = 0;
46
47         if (node->optional_number_child<int>("UTCOffset")) {
48                 hour = node->number_child<int>("UTCOffset");
49         } else {
50                 hour = node->optional_number_child<int>("UTCOffsetHour").get_value_or(0);
51         }
52
53         int minute = node->optional_number_child<int>("UTCOffsetMinute").get_value_or(0);
54
55         utc_offset= { hour, minute };
56 }
57
58 /* This is necessary so that we can use shared_from_this in add_screen (which cannot be done from
59    a constructor)
60 */
61 void
62 Cinema::read_screens (cxml::ConstNodePtr node)
63 {
64         for (auto i: node->node_children("Screen")) {
65                 add_screen (make_shared<Screen>(i));
66         }
67 }
68
69 void
70 Cinema::as_xml (xmlpp::Element* parent) const
71 {
72         cxml::add_text_child(parent, "Name", name);
73
74         for (auto i: emails) {
75                 cxml::add_text_child(parent, "Email", i);
76         }
77
78         cxml::add_text_child(parent, "Notes", notes);
79
80         cxml::add_text_child(parent, "UTCOffsetHour", raw_convert<string>(utc_offset.hour()));
81         cxml::add_text_child(parent, "UTCOffsetMinute", raw_convert<string>(utc_offset.minute()));
82
83         for (auto i: _screens) {
84                 i->as_xml(cxml::add_child(parent, "Screen"));
85         }
86 }
87
88 void
89 Cinema::add_screen (shared_ptr<Screen> s)
90 {
91         s->cinema = shared_from_this ();
92         _screens.push_back (s);
93 }
94
95 void
96 Cinema::remove_screen (shared_ptr<Screen> s)
97 {
98        auto iter = std::find(_screens.begin(), _screens.end(), s);
99        if (iter != _screens.end()) {
100                _screens.erase(iter);
101        }
102 }
103