Display the filename / URL that a screen certificate was obtained from (#1894).
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-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 "screens_panel.h"
23 #include "wx_util.h"
24 #include "cinema_dialog.h"
25 #include "screen_dialog.h"
26 #include "dcpomatic_button.h"
27 #include "lib/config.h"
28 #include "lib/cinema.h"
29 #include "lib/screen.h"
30
31
32 using std::list;
33 using std::pair;
34 using std::cout;
35 using std::map;
36 using std::string;
37 using std::make_pair;
38 using std::make_shared;
39 using std::shared_ptr;
40 using boost::optional;
41 using namespace dcpomatic;
42
43
44 ScreensPanel::ScreensPanel (wxWindow* parent)
45         : wxPanel (parent, wxID_ANY)
46         , _ignore_selection_change (false)
47 {
48         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
49
50 #ifdef __WXGTK3__
51         int const height = 30;
52 #else
53         int const height = -1;
54 #endif
55
56         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
57 #ifndef __WXGTK3__
58         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
59         _search->ShowCancelButton (true);
60 #endif
61         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
62
63         auto targets = new wxBoxSizer (wxHORIZONTAL);
64         _targets = new TreeCtrl (this);
65         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
66
67         add_cinemas ();
68
69         auto target_buttons = new wxBoxSizer (wxVERTICAL);
70
71         _add_cinema = new Button (this, _("Add Cinema..."));
72         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73         _edit_cinema = new Button (this, _("Edit Cinema..."));
74         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
75         _remove_cinema = new Button (this, _("Remove Cinema"));
76         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77         _add_screen = new Button (this, _("Add Screen..."));
78         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79         _edit_screen = new Button (this, _("Edit Screen..."));
80         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81         _remove_screen = new Button (this, _("Remove Screen"));
82         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
83
84         targets->Add (target_buttons, 0, 0);
85
86         sizer->Add (targets, 1, wxEXPAND);
87
88         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
89         _targets->Bind       (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
90
91         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
92         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
93         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
94
95         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
96         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
97         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
98
99         SetSizer (sizer);
100 }
101
102
103 ScreensPanel::~ScreensPanel ()
104 {
105         _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
106 }
107
108
109 void
110 ScreensPanel::setup_sensitivity ()
111 {
112         bool const sc = _selected_cinemas.size() == 1;
113         bool const ss = _selected_screens.size() == 1;
114
115         _edit_cinema->Enable (sc);
116         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
117
118         _add_screen->Enable (sc);
119         _edit_screen->Enable (ss);
120         _remove_screen->Enable (_selected_screens.size() >= 1);
121 }
122
123
124 optional<wxTreeItemId>
125 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
126 {
127         auto search = wx_to_std (_search->GetValue ());
128         transform (search.begin(), search.end(), search.begin(), ::tolower);
129
130         if (!search.empty ()) {
131                 auto name = c->name;
132                 transform (name.begin(), name.end(), name.begin(), ::tolower);
133                 if (name.find (search) == string::npos) {
134                         return optional<wxTreeItemId>();
135                 }
136         }
137
138         auto id = _targets->AppendItem(_root, std_to_wx(c->name));
139
140         _cinemas[id] = c;
141
142         for (auto i: c->screens()) {
143                 add_screen (c, i);
144         }
145
146         _targets->SortChildren (_root);
147
148         return id;
149 }
150
151
152 optional<wxTreeItemId>
153 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
154 {
155         auto i = _cinemas.begin();
156         while (i != _cinemas.end() && i->second != c) {
157                 ++i;
158         }
159
160         if (i == _cinemas.end()) {
161                 return {};
162         }
163
164         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
165         return i->first;
166 }
167
168
169 void
170 ScreensPanel::add_cinema_clicked ()
171 {
172         auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
173         if (d->ShowModal () == wxID_OK) {
174                 auto c = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
175                 Config::instance()->add_cinema (c);
176                 auto id = add_cinema (c);
177                 if (id) {
178                         _targets->Unselect ();
179                         _targets->SelectItem (*id);
180                 }
181         }
182
183         d->Destroy ();
184 }
185
186
187 void
188 ScreensPanel::edit_cinema_clicked ()
189 {
190         if (_selected_cinemas.size() != 1) {
191                 return;
192         }
193
194         auto c = *_selected_cinemas.begin();
195
196         auto d = new CinemaDialog (
197                 GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
198                 );
199
200         if (d->ShowModal() == wxID_OK) {
201                 c.second->name = d->name ();
202                 c.second->emails = d->emails ();
203                 c.second->notes = d->notes ();
204                 c.second->set_utc_offset_hour (d->utc_offset_hour ());
205                 c.second->set_utc_offset_minute (d->utc_offset_minute ());
206                 _targets->SetItemText (c.first, std_to_wx (d->name()));
207                 Config::instance()->changed (Config::CINEMAS);
208         }
209
210         d->Destroy ();
211 }
212
213
214 void
215 ScreensPanel::remove_cinema_clicked ()
216 {
217         if (_selected_cinemas.size() == 1) {
218                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
219                         return;
220                 }
221         } else {
222                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
223                         return;
224                 }
225         }
226
227         for (auto const& i: _selected_cinemas) {
228                 Config::instance()->remove_cinema (i.second);
229                 _targets->Delete (i.first);
230         }
231
232         selection_changed ();
233 }
234
235
236 void
237 ScreensPanel::add_screen_clicked ()
238 {
239         if (_selected_cinemas.size() != 1) {
240                 return;
241         }
242
243         auto c = _selected_cinemas.begin()->second;
244
245         auto d = new ScreenDialog (GetParent(), _("Add Screen"));
246         if (d->ShowModal () != wxID_OK) {
247                 d->Destroy ();
248                 return;
249         }
250
251         for (auto i: c->screens ()) {
252                 if (i->name == d->name()) {
253                         error_dialog (
254                                 GetParent(),
255                                 wxString::Format (
256                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
257                                         std_to_wx(d->name()).data()
258                                         )
259                                 );
260                         return;
261                 }
262         }
263
264         auto s = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
265         c->add_screen (s);
266         auto id = add_screen (c, s);
267         if (id) {
268                 _targets->Expand (id.get ());
269         }
270
271         Config::instance()->changed (Config::CINEMAS);
272
273         d->Destroy ();
274 }
275
276
277 void
278 ScreensPanel::edit_screen_clicked ()
279 {
280         if (_selected_screens.size() != 1) {
281                 return;
282         }
283
284         auto s = *_selected_screens.begin();
285
286         auto d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->recipient_file, s.second->trusted_devices);
287         if (d->ShowModal() != wxID_OK) {
288                 d->Destroy ();
289                 return;
290         }
291
292         auto c = s.second->cinema;
293         for (auto i: c->screens ()) {
294                 if (i != s.second && i->name == d->name()) {
295                         error_dialog (
296                                 GetParent(),
297                                 wxString::Format (
298                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
299                                         std_to_wx(d->name()).data()
300                                         )
301                                 );
302                         return;
303                 }
304         }
305
306         s.second->name = d->name ();
307         s.second->notes = d->notes ();
308         s.second->recipient = d->recipient ();
309         s.second->recipient_file = d->recipient_file ();
310         s.second->trusted_devices = d->trusted_devices ();
311         _targets->SetItemText (s.first, std_to_wx (d->name()));
312         Config::instance()->changed (Config::CINEMAS);
313
314         d->Destroy ();
315 }
316
317
318 void
319 ScreensPanel::remove_screen_clicked ()
320 {
321         if (_selected_screens.size() == 1) {
322                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
323                         return;
324                 }
325         } else {
326                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
327                         return;
328                 }
329         }
330
331         for (auto const& i: _selected_screens) {
332                 auto j = _cinemas.begin ();
333                 while (j != _cinemas.end ()) {
334                         auto sc = j->second->screens ();
335                         if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
336                                 break;
337                         }
338
339                         ++j;
340                 }
341
342                 if (j == _cinemas.end()) {
343                         continue;
344                 }
345
346                 j->second->remove_screen (i.second);
347                 _targets->Delete (i.first);
348         }
349
350         Config::instance()->changed (Config::CINEMAS);
351 }
352
353
354 list<shared_ptr<Screen>>
355 ScreensPanel::screens () const
356 {
357         list<shared_ptr<Screen>> s;
358
359         for (auto const& i: _selected_cinemas) {
360                 for (auto j: i.second->screens()) {
361                         s.push_back (j);
362                 }
363         }
364
365         for (auto const& i: _selected_screens) {
366                 s.push_back (i.second);
367         }
368
369         s.sort ();
370         s.unique ();
371
372         return s;
373 }
374
375
376 void
377 ScreensPanel::selection_changed_shim (wxTreeEvent &)
378 {
379         selection_changed ();
380 }
381
382
383 void
384 ScreensPanel::selection_changed ()
385 {
386         if (_ignore_selection_change) {
387                 return;
388         }
389
390         wxArrayTreeItemIds s;
391         _targets->GetSelections (s);
392
393         _selected_cinemas.clear ();
394         _selected_screens.clear ();
395
396         for (size_t i = 0; i < s.GetCount(); ++i) {
397                 auto j = _cinemas.find (s[i]);
398                 if (j != _cinemas.end ()) {
399                         _selected_cinemas[j->first] = j->second;
400                 }
401                 auto k = _screens.find (s[i]);
402                 if (k != _screens.end ()) {
403                         _selected_screens[k->first] = k->second;
404                 }
405         }
406
407         setup_sensitivity ();
408         ScreensChanged ();
409 }
410
411
412 void
413 ScreensPanel::add_cinemas ()
414 {
415         _root = _targets->AddRoot ("Foo");
416
417         for (auto i: Config::instance()->cinemas()) {
418                 add_cinema (i);
419         }
420 }
421
422
423 void
424 ScreensPanel::search_changed ()
425 {
426         _targets->DeleteAllItems ();
427         _cinemas.clear ();
428         _screens.clear ();
429
430         add_cinemas ();
431
432         _ignore_selection_change = true;
433
434         for (auto const& i: _selected_cinemas) {
435                 /* The wxTreeItemIds will now be different, so we must search by cinema */
436                 auto j = _cinemas.begin ();
437                 while (j != _cinemas.end() && j->second != i.second) {
438                         ++j;
439                 }
440
441                 if (j != _cinemas.end()) {
442                         _targets->SelectItem (j->first);
443                 }
444         }
445
446         for (auto const& i: _selected_screens) {
447                 auto j = _screens.begin ();
448                 while (j != _screens.end() && j->second != i.second) {
449                         ++j;
450                 }
451
452                 if (j != _screens.end()) {
453                         _targets->SelectItem (j->first);
454                 }
455         }
456
457         _ignore_selection_change = false;
458 }
459
460
461 wxIMPLEMENT_DYNAMIC_CLASS (TreeCtrl, wxTreeCtrl);
462
463
464 int
465 TreeCtrl::OnCompareItems (wxTreeItemId const& a, wxTreeItemId const& b)
466 {
467         return strcoll (wx_to_std(GetItemText(a)).c_str(), wx_to_std(GetItemText(b)).c_str());
468 }
469