Missing i18n tags.
[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         wxGridSizer* target_buttons = new wxGridSizer (2, DCPOMATIC_BUTTON_STACK_GAP * 2, DCPOMATIC_SIZER_Y_GAP);
55
56         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
57         target_buttons->Add (_add_cinema, 1, wxEXPAND);
58         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
59         target_buttons->Add (_add_screen, 1, wxEXPAND);
60         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
61         target_buttons->Add (_edit_cinema, 1, wxEXPAND);
62         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
63         target_buttons->Add (_edit_screen, 1, wxEXPAND);
64         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
65         target_buttons->Add (_remove_cinema, 1, wxEXPAND);
66         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
67         target_buttons->Add (_remove_screen, 1, wxEXPAND);
68
69         targets->Add (target_buttons, 0, 0);
70
71         sizer->Add (targets, 1, wxEXPAND);
72
73         _search->Bind        (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
74         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
75
76         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
77         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
78         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
79
80         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
81         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
82         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
83
84         SetSizer (sizer);
85 }
86
87 ScreensPanel::~ScreensPanel ()
88 {
89         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
90 }
91
92 void
93 ScreensPanel::setup_sensitivity ()
94 {
95         bool const sc = _selected_cinemas.size() == 1;
96         bool const ss = _selected_screens.size() == 1;
97
98         _edit_cinema->Enable (sc);
99         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
100
101         _add_screen->Enable (sc);
102         _edit_screen->Enable (ss);
103         _remove_screen->Enable (_selected_screens.size() >= 1);
104 }
105
106 void
107 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
108 {
109         string search = wx_to_std (_search->GetValue ());
110         transform (search.begin(), search.end(), search.begin(), ::tolower);
111
112         if (!search.empty ()) {
113                 string name = c->name;
114                 transform (name.begin(), name.end(), name.begin(), ::tolower);
115                 if (name.find (search) == string::npos) {
116                         return;
117                 }
118         }
119
120         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
121
122         list<shared_ptr<Screen> > sc = c->screens ();
123         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
124                 add_screen (c, *i);
125         }
126
127         _targets->SortChildren (_root);
128 }
129
130 optional<wxTreeItemId>
131 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
132 {
133         CinemaMap::const_iterator i = _cinemas.begin();
134         while (i != _cinemas.end() && i->second != c) {
135                 ++i;
136         }
137
138         if (i == _cinemas.end()) {
139                 return optional<wxTreeItemId> ();
140         }
141
142         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
143         return i->first;
144 }
145
146 void
147 ScreensPanel::add_cinema_clicked ()
148 {
149         CinemaDialog* d = new CinemaDialog (this, _("Add Cinema"));
150         if (d->ShowModal () == wxID_OK) {
151                 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->utc_offset_hour(), d->utc_offset_minute()));
152                 Config::instance()->add_cinema (c);
153                 add_cinema (c);
154         }
155
156         d->Destroy ();
157 }
158
159 void
160 ScreensPanel::edit_cinema_clicked ()
161 {
162         if (_selected_cinemas.size() != 1) {
163                 return;
164         }
165
166         pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
167
168         CinemaDialog* d = new CinemaDialog (
169                 this, _("Edit cinema"), c.second->name, c.second->emails, c.second->utc_offset_hour(), c.second->utc_offset_minute()
170                 );
171
172         if (d->ShowModal () == wxID_OK) {
173                 c.second->name = d->name ();
174                 c.second->emails = d->emails ();
175                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
176                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
177                 _targets->SetItemText (c.first, std_to_wx (d->name()));
178                 Config::instance()->changed ();
179         }
180
181         d->Destroy ();
182 }
183
184 void
185 ScreensPanel::remove_cinema_clicked ()
186 {
187         for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
188                 Config::instance()->remove_cinema (i->second);
189                 _targets->Delete (i->first);
190         }
191
192         selection_changed ();
193 }
194
195 void
196 ScreensPanel::add_screen_clicked ()
197 {
198         if (_selected_cinemas.size() != 1) {
199                 return;
200         }
201
202         shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
203
204         ScreenDialog* d = new ScreenDialog (this, _("Add Screen"));
205         if (d->ShowModal () != wxID_OK) {
206                 return;
207         }
208
209         shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
210         c->add_screen (s);
211         optional<wxTreeItemId> id = add_screen (c, s);
212         if (id) {
213                 _targets->Expand (id.get ());
214         }
215
216         Config::instance()->changed ();
217
218         d->Destroy ();
219 }
220
221 void
222 ScreensPanel::edit_screen_clicked ()
223 {
224         if (_selected_screens.size() != 1) {
225                 return;
226         }
227
228         pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
229
230         ScreenDialog* d = new ScreenDialog (this, _("Edit screen"), s.second->name, s.second->recipient, s.second->trusted_devices);
231         if (d->ShowModal () == wxID_OK) {
232                 s.second->name = d->name ();
233                 s.second->recipient = d->recipient ();
234                 s.second->trusted_devices = d->trusted_devices ();
235                 _targets->SetItemText (s.first, std_to_wx (d->name()));
236                 Config::instance()->changed ();
237         }
238
239         d->Destroy ();
240 }
241
242 void
243 ScreensPanel::remove_screen_clicked ()
244 {
245         for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
246                 CinemaMap::iterator j = _cinemas.begin ();
247                 while (j != _cinemas.end ()) {
248                         list<shared_ptr<Screen> > sc = j->second->screens ();
249                         if (find (sc.begin(), sc.end(), i->second) != sc.end ()) {
250                                 break;
251                         }
252
253                         ++j;
254                 }
255
256                 if (j == _cinemas.end()) {
257                         continue;
258                 }
259
260                 j->second->remove_screen (i->second);
261                 _targets->Delete (i->first);
262         }
263
264         Config::instance()->changed ();
265 }
266
267 list<shared_ptr<Screen> >
268 ScreensPanel::screens () const
269 {
270         list<shared_ptr<Screen> > s;
271
272         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
273                 list<shared_ptr<Screen> > sc = i->second->screens ();
274                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
275                         s.push_back (*j);
276                 }
277         }
278
279         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
280                 s.push_back (i->second);
281         }
282
283         s.sort ();
284         s.unique ();
285
286         return s;
287 }
288
289 void
290 ScreensPanel::selection_changed_shim (wxTreeEvent &)
291 {
292         selection_changed ();
293 }
294
295 void
296 ScreensPanel::selection_changed ()
297 {
298         if (_ignore_selection_change) {
299                 return;
300         }
301
302         wxArrayTreeItemIds s;
303         _targets->GetSelections (s);
304
305         _selected_cinemas.clear ();
306         _selected_screens.clear ();
307
308         for (size_t i = 0; i < s.GetCount(); ++i) {
309                 CinemaMap::const_iterator j = _cinemas.find (s[i]);
310                 if (j != _cinemas.end ()) {
311                         _selected_cinemas[j->first] = j->second;
312                 }
313                 ScreenMap::const_iterator k = _screens.find (s[i]);
314                 if (k != _screens.end ()) {
315                         _selected_screens[k->first] = k->second;
316                 }
317         }
318
319         setup_sensitivity ();
320         ScreensChanged ();
321 }
322
323 void
324 ScreensPanel::add_cinemas ()
325 {
326         _root = _targets->AddRoot ("Foo");
327
328         BOOST_FOREACH (shared_ptr<Cinema> i, Config::instance()->cinemas ()) {
329                 add_cinema (i);
330         }
331 }
332
333 void
334 ScreensPanel::search_changed ()
335 {
336         _targets->DeleteAllItems ();
337         _cinemas.clear ();
338         _screens.clear ();
339
340         add_cinemas ();
341
342         _ignore_selection_change = true;
343
344         for (CinemaMap::const_iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
345                 /* The wxTreeItemIds will now be different, so we must search by cinema */
346                 CinemaMap::const_iterator j = _cinemas.begin ();
347                 while (j != _cinemas.end() && j->second != i->second) {
348                         ++j;
349                 }
350
351                 if (j != _cinemas.end ()) {
352                         _targets->SelectItem (j->first);
353                 }
354         }
355
356         for (ScreenMap::const_iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
357                 ScreenMap::const_iterator j = _screens.begin ();
358                 while (j != _screens.end() && j->second != i->second) {
359                         ++j;
360                 }
361
362                 if (j != _screens.end ()) {
363                         _targets->SelectItem (j->first);
364                 }
365         }
366
367         _ignore_selection_change = false;
368 }