Add UTC-3:30 timezone to cinema (#831).
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/config.h"
21 #include "lib/cinema.h"
22 #include "lib/screen.h"
23 #include "screens_panel.h"
24 #include "wx_util.h"
25 #include "cinema_dialog.h"
26 #include "screen_dialog.h"
27 #include <boost/foreach.hpp>
28
29 using std::list;
30 using std::pair;
31 using std::cout;
32 using std::map;
33 using std::string;
34 using std::make_pair;
35 using boost::shared_ptr;
36 using boost::optional;
37
38 ScreensPanel::ScreensPanel (wxWindow* parent)
39         : wxPanel (parent, wxID_ANY)
40         , _ignore_selection_change (false)
41 {
42         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
43
44         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
45         _search->ShowCancelButton (true);
46         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
47
48         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
49         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
50         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
51
52         add_cinemas ();
53
54         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
55
56         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
57         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
58         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
59         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
60         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
61         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
62
63         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
64         target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
65         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
66         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
67         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
68         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
69
70         targets->Add (target_buttons, 0, 0);
71
72         sizer->Add (targets, 1, wxEXPAND);
73
74         _search->Bind        (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
75         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
76
77         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
78         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
79         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
80
81         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
82         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
83         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
84
85         SetSizer (sizer);
86 }
87
88 ScreensPanel::~ScreensPanel ()
89 {
90         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
91 }
92
93 void
94 ScreensPanel::setup_sensitivity ()
95 {
96         bool const sc = _selected_cinemas.size() == 1;
97         bool const ss = _selected_screens.size() == 1;
98
99         _edit_cinema->Enable (sc);
100         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
101
102         _add_screen->Enable (sc);
103         _edit_screen->Enable (ss);
104         _remove_screen->Enable (_selected_screens.size() >= 1);
105 }
106
107 void
108 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
109 {
110         string search = wx_to_std (_search->GetValue ());
111         transform (search.begin(), search.end(), search.begin(), ::tolower);
112
113         if (!search.empty ()) {
114                 string name = c->name;
115                 transform (name.begin(), name.end(), name.begin(), ::tolower);
116                 if (name.find (search) == string::npos) {
117                         return;
118                 }
119         }
120
121         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
122
123         list<shared_ptr<Screen> > sc = c->screens ();
124         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
125                 add_screen (c, *i);
126         }
127
128         _targets->SortChildren (_root);
129 }
130
131 optional<wxTreeItemId>
132 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
133 {
134         CinemaMap::const_iterator i = _cinemas.begin();
135         while (i != _cinemas.end() && i->second != c) {
136                 ++i;
137         }
138
139         if (i == _cinemas.end()) {
140                 return optional<wxTreeItemId> ();
141         }
142
143         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
144         return i->first;
145 }
146
147 void
148 ScreensPanel::add_cinema_clicked ()
149 {
150         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
151         if (d->ShowModal () == wxID_OK) {
152                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->utc_offset_hour(), d->utc_offset_minute()));
153                 Config::instance()->add_cinema (c);
154                 add_cinema (c);
155         }
156
157         d->Destroy ();
158 }
159
160 void
161 ScreensPanel::edit_cinema_clicked ()
162 {
163         if (_selected_cinemas.size() != 1) {
164                 return;
165         }
166
167         pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
168
169         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->emails, c.second->utc_offset_hour(), c.second->utc_offset_minute());
170         if (d->ShowModal () == wxID_OK) {
171                 c.second->name = d->name ();
172                 c.second->emails = d->emails ();
173                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
174                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
175                 _targets->SetItemText (c.first, std_to_wx (d->name()));
176                 Config::instance()->changed ();
177         }
178
179         d->Destroy ();
180 }
181
182 void
183 ScreensPanel::remove_cinema_clicked ()
184 {
185         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
186                 Config::instance()->remove_cinema (i->second);
187                 _targets->Delete (i->first);
188         }
189
190         selection_changed ();
191 }
192
193 void
194 ScreensPanel::add_screen_clicked ()
195 {
196         if (_selected_cinemas.size() != 1) {
197                 return;
198         }
199
200         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
201
202         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
203         if (d->ShowModal () != wxID_OK) {
204                 return;
205         }
206
207         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
208         c->add_screen (s);
209         optional<wxTreeItemId> id = add_screen (c, s);
210         if (id) {
211                 _targets->Expand (id.get ());
212         }
213
214         Config::instance()->changed ();
215
216         d->Destroy ();
217 }
218
219 void
220 ScreensPanel::edit_screen_clicked ()
221 {
222         if (_selected_screens.size() != 1) {
223                 return;
224         }
225
226         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
227
228         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient, s.second->trusted_devices);
229         if (d->ShowModal () == wxID_OK) {
230                 s.second->name = d->name ();
231                 s.second->recipient = d->recipient ();
232                 s.second->trusted_devices = d->trusted_devices ();
233                 _targets->SetItemText (s.first, std_to_wx (d->name()));
234                 Config::instance()->changed ();
235         }
236
237         d->Destroy ();
238 }
239
240 void
241 ScreensPanel::remove_screen_clicked ()
242 {
243         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
244                 CinemaMap::iterator j = _cinemas.begin ();
245                 while (j != _cinemas.end ()) {
246                         list<shared_ptr<Screen> > sc = j->second->screens ();
247                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
248                                 break;
249                         }
250
251                         ++j;
252                 }
253
254                 if (j == _cinemas.end()) {
255                         continue;
256                 }
257
258                 j->second->remove_screen (i->second);
259                 _targets->Delete (i->first);
260         }
261
262         Config::instance()->changed ();
263 }
264
265 list<shared_ptr<Screen> >
266 ScreensPanel::screens () const
267 {
268         list<shared_ptr<Screen> > s;
269
270         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
271                 list<shared_ptr<Screen> > sc = i->second->screens ();
272                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
273                         s.push_back (*j);
274                 }
275         }
276
277         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
278                 s.push_back (i->second);
279         }
280
281         s.sort ();
282         s.unique ();
283
284         return s;
285 }
286
287 void
288 ScreensPanel::selection_changed_shim (wxTreeEvent &)
289 {
290         selection_changed ();
291 }
292
293 void
294 ScreensPanel::selection_changed ()
295 {
296         if (_ignore_selection_change) {
297                 return;
298         }
299
300         wxArrayTreeItemIds s;
301         _targets->GetSelections (s);
302
303         _selected_cinemas.clear ();
304         _selected_screens.clear ();
305
306         for (size_t i = 0; i < s.GetCount(); ++i) {
307                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
308                 if (j != _cinemas.end ()) {
309                         _selected_cinemas[j->first] = j->second;
310                 }
311                 ScreenMap::const_iterator k = _screens.find (s[i]);
312                 if (k != _screens.end ()) {
313                         _selected_screens[k->first] = k->second;
314                 }
315         }
316
317         setup_sensitivity ();
318         ScreensChanged ();
319 }
320
321 void
322 ScreensPanel::add_cinemas ()
323 {
324         _root = _targets->AddRoot ("Foo");
325
326         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
327                 add_cinema (i);
328         }
329 }
330
331 void
332 ScreensPanel::search_changed ()
333 {
334         _targets->DeleteAllItems ();
335         _cinemas.clear ();
336         _screens.clear ();
337
338         add_cinemas ();
339
340         _ignore_selection_change = true;
341
342         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
343                 /* The wxTreeItemIds will now be different, so we must search by cinema */
344                 CinemaMap::const_iterator j = _cinemas.begin ();
345                 while (j != _cinemas.end() && j->second != i->second) {
346                         ++j;
347                 }
348
349                 if (j != _cinemas.end ()) {
350                         _targets->SelectItem (j->first);
351                 }
352         }
353
354         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
355                 ScreenMap::const_iterator j = _screens.begin ();
356                 while (j != _screens.end() && j->second != i->second) {
357                         ++j;
358                 }
359
360                 if (j != _screens.end ()) {
361                         _targets->SelectItem (j->first);
362                 }
363         }
364
365         _ignore_selection_change = false;
366 }