Revert "Use make_shared<>."
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-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 "lib/config.h"
22 #include "lib/cinema.h"
23 #include "lib/screen.h"
24 #include "screens_panel.h"
25 #include "wx_util.h"
26 #include "cinema_dialog.h"
27 #include "screen_dialog.h"
28 #include <boost/foreach.hpp>
29
30 using std::list;
31 using std::pair;
32 using std::cout;
33 using std::map;
34 using std::string;
35 using std::make_pair;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 ScreensPanel::ScreensPanel (wxWindow* parent)
40         : wxPanel (parent, wxID_ANY)
41         , _ignore_selection_change (false)
42 {
43         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
44
45         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
46         _search->ShowCancelButton (true);
47         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
48
49         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
50         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
51         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
52
53         add_cinemas ();
54
55         wxGridSizer* target_buttons = new wxGridSizer (2, DCPOMATIC_BUTTON_STACK_GAP * 2, DCPOMATIC_SIZER_Y_GAP);
56
57         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
58         target_buttons->Add (_add_cinema, 1, wxEXPAND);
59         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
60         target_buttons->Add (_add_screen, 1, wxEXPAND);
61         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
62         target_buttons->Add (_edit_cinema, 1, wxEXPAND);
63         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
64         target_buttons->Add (_edit_screen, 1, wxEXPAND);
65         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
66         target_buttons->Add (_remove_cinema, 1, wxEXPAND);
67         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
68         target_buttons->Add (_remove_screen, 1, wxEXPAND);
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->notes(), 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 (
170                 this, _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
171                 );
172
173         if (d->ShowModal () == wxID_OK) {
174                 c.second->name = d->name ();
175                 c.second->emails = d->emails ();
176                 c.second->notes = d->notes ();
177                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
178                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
179                 _targets->SetItemText (c.first, std_to_wx (d->name()));
180                 Config::instance()->changed ();
181         }
182
183         d->Destroy ();
184 }
185
186 void
187 ScreensPanel::remove_cinema_clicked ()
188 {
189         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
190                 Config::instance()->remove_cinema (i->second);
191                 _targets->Delete (i->first);
192         }
193
194         selection_changed ();
195 }
196
197 void
198 ScreensPanel::add_screen_clicked ()
199 {
200         if (_selected_cinemas.size() != 1) {
201                 return;
202         }
203
204         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
205
206         ScreenDialog* d = new ScreenDialog (this, _("Add Screen"));
207         if (d->ShowModal () != wxID_OK) {
208                 return;
209         }
210
211         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
212         c->add_screen (s);
213         optional<wxTreeItemId> id = add_screen (c, s);
214         if (id) {
215                 _targets->Expand (id.get ());
216         }
217
218         Config::instance()->changed ();
219
220         d->Destroy ();
221 }
222
223 void
224 ScreensPanel::edit_screen_clicked ()
225 {
226         if (_selected_screens.size() != 1) {
227                 return;
228         }
229
230         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
231
232         ScreenDialog* d = new ScreenDialog (this, _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
233         if (d->ShowModal () == wxID_OK) {
234                 s.second->name = d->name ();
235                 s.second->notes = d->notes ();
236                 s.second->recipient = d->recipient ();
237                 s.second->trusted_devices = d->trusted_devices ();
238                 _targets->SetItemText (s.first, std_to_wx (d->name()));
239                 Config::instance()->changed ();
240         }
241
242         d->Destroy ();
243 }
244
245 void
246 ScreensPanel::remove_screen_clicked ()
247 {
248         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
249                 CinemaMap::iterator j = _cinemas.begin ();
250                 while (j != _cinemas.end ()) {
251                         list<shared_ptr<Screen> > sc = j->second->screens ();
252                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
253                                 break;
254                         }
255
256                         ++j;
257                 }
258
259                 if (j == _cinemas.end()) {
260                         continue;
261                 }
262
263                 j->second->remove_screen (i->second);
264                 _targets->Delete (i->first);
265         }
266
267         Config::instance()->changed ();
268 }
269
270 list<shared_ptr<Screen> >
271 ScreensPanel::screens () const
272 {
273         list<shared_ptr<Screen> > s;
274
275         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
276                 list<shared_ptr<Screen> > sc = i->second->screens ();
277                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
278                         s.push_back (*j);
279                 }
280         }
281
282         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
283                 s.push_back (i->second);
284         }
285
286         s.sort ();
287         s.unique ();
288
289         return s;
290 }
291
292 void
293 ScreensPanel::selection_changed_shim (wxTreeEvent &)
294 {
295         selection_changed ();
296 }
297
298 void
299 ScreensPanel::selection_changed ()
300 {
301         if (_ignore_selection_change) {
302                 return;
303         }
304
305         wxArrayTreeItemIds s;
306         _targets->GetSelections (s);
307
308         _selected_cinemas.clear ();
309         _selected_screens.clear ();
310
311         for (size_t i = 0; i < s.GetCount(); ++i) {
312                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
313                 if (j != _cinemas.end ()) {
314                         _selected_cinemas[j->first] = j->second;
315                 }
316                 ScreenMap::const_iterator k = _screens.find (s[i]);
317                 if (k != _screens.end ()) {
318                         _selected_screens[k->first] = k->second;
319                 }
320         }
321
322         setup_sensitivity ();
323         ScreensChanged ();
324 }
325
326 void
327 ScreensPanel::add_cinemas ()
328 {
329         _root = _targets->AddRoot ("Foo");
330
331         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
332                 add_cinema (i);
333         }
334 }
335
336 void
337 ScreensPanel::search_changed ()
338 {
339         _targets->DeleteAllItems ();
340         _cinemas.clear ();
341         _screens.clear ();
342
343         add_cinemas ();
344
345         _ignore_selection_change = true;
346
347         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
348                 /* The wxTreeItemIds will now be different, so we must search by cinema */
349                 CinemaMap::const_iterator j = _cinemas.begin ();
350                 while (j != _cinemas.end() && j->second != i->second) {
351                         ++j;
352                 }
353
354                 if (j != _cinemas.end ()) {
355                         _targets->SelectItem (j->first);
356                 }
357         }
358
359         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
360                 ScreenMap::const_iterator j = _screens.begin ();
361                 while (j != _screens.end() && j->second != i->second) {
362                         ++j;
363                 }
364
365                 if (j != _screens.end ()) {
366                         _targets->SelectItem (j->first);
367                 }
368         }
369
370         _ignore_selection_change = false;
371 }