Stack-allocate wxDialog instead of using wx_ptr.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-2022 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_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.h"
26 #include "wx_util.h"
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/scope_guard.h"
30 #include "lib/screen.h"
31 #include "lib/timer.h"
32
33
34 using std::cout;
35 using std::list;
36 using std::make_pair;
37 using std::make_shared;
38 using std::map;
39 using std::pair;
40 using std::shared_ptr;
41 using std::string;
42 using std::vector;
43 using boost::optional;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
46 #endif
47 using namespace dcpomatic;
48
49
50 ScreensPanel::ScreensPanel (wxWindow* parent)
51         : wxPanel (parent, wxID_ANY)
52 {
53         auto sizer = new wxBoxSizer (wxVERTICAL);
54
55         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
56 #ifndef __WXGTK3__
57         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
58         _search->ShowCancelButton (true);
59 #endif
60         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
61
62         auto targets = new wxBoxSizer (wxHORIZONTAL);
63         _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
64         _targets->AppendColumn (wxT("foo"));
65
66         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
67
68         add_cinemas ();
69
70         auto side_buttons = new wxBoxSizer (wxVERTICAL);
71
72         auto target_buttons = new wxBoxSizer (wxVERTICAL);
73
74         _add_cinema = new Button (this, _("Add Cinema..."));
75         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76         _edit_cinema = new Button (this, _("Edit Cinema..."));
77         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78         _remove_cinema = new Button (this, _("Remove Cinema"));
79         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
80         _add_screen = new Button (this, _("Add Screen..."));
81         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
82         _edit_screen = new Button (this, _("Edit Screen..."));
83         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84         _remove_screen = new Button (this, _("Remove Screen"));
85         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
86
87         side_buttons->Add (target_buttons, 0, 0);
88
89         auto check_buttons = new wxBoxSizer (wxVERTICAL);
90
91         _check_all = new Button (this, _("Check all"));
92         check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
93         _uncheck_all = new Button (this, _("Uncheck all"));
94         check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
95
96         side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
97
98         targets->Add (side_buttons, 0, 0);
99
100         sizer->Add (targets, 1, wxEXPAND);
101
102         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
103         _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
104         _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
105
106         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
107         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
108         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
109
110         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
111         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
112         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
113
114         _check_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
115         _uncheck_all->Bind   (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
116
117         SetSizer (sizer);
118
119         _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
120 }
121
122
123 ScreensPanel::~ScreensPanel ()
124 {
125         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
126         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
127 }
128
129
130 void
131 ScreensPanel::check_all ()
132 {
133         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
134                 _targets->CheckItem(cinema, wxCHK_CHECKED);
135                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
136                         _targets->CheckItem(screen, wxCHK_CHECKED);
137                         set_screen_checked(screen, true);
138                 }
139         }
140 }
141
142
143 void
144 ScreensPanel::uncheck_all ()
145 {
146         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
147                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
148                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
149                         _targets->CheckItem(screen, wxCHK_UNCHECKED);
150                         set_screen_checked(screen, false);
151                 }
152         }
153 }
154
155
156 void
157 ScreensPanel::setup_sensitivity ()
158 {
159         bool const sc = _selected_cinemas.size() == 1;
160         bool const ss = _selected_screens.size() == 1;
161
162         _edit_cinema->Enable (sc || ss);
163         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
164
165         _add_screen->Enable (sc || ss);
166         _edit_screen->Enable (ss);
167         _remove_screen->Enable (_selected_screens.size() >= 1);
168 }
169
170
171 void
172 ScreensPanel::convert_to_lower(string& s)
173 {
174         transform(s.begin(), s.end(), s.begin(), ::tolower);
175 }
176
177
178 bool
179 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string lower_case_search)
180 {
181         if (lower_case_search.empty()) {
182                 return true;
183         }
184
185         auto name = cinema->name;
186         convert_to_lower(name);
187         return name.find(lower_case_search) != string::npos;
188 }
189
190
191 optional<wxTreeListItem>
192 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
193 {
194         auto search = wx_to_std (_search->GetValue ());
195         convert_to_lower(search);
196         if (!matches_search(cinema, search)) {
197                 return {};
198         }
199
200         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
201
202         _item_to_cinema[id] = cinema;
203         _cinema_to_item[cinema] = id;
204
205         for (auto screen: cinema->screens()) {
206                 add_screen (cinema, screen);
207         }
208
209         return id;
210 }
211
212
213 optional<wxTreeListItem>
214 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
215 {
216         auto item = cinema_to_item(cinema);
217         if (!item) {
218                 return {};
219         }
220
221         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
222
223         _item_to_screen[id] = screen;
224         _screen_to_item[screen] = id;
225
226         return item;
227 }
228
229
230 void
231 ScreensPanel::add_cinema_clicked ()
232 {
233         CinemaDialog dialog(GetParent(), _("Add Cinema"));
234
235         if (dialog.ShowModal() == wxID_OK) {
236                 auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
237
238                 auto cinemas = sorted_cinemas();
239
240                 try {
241                         _ignore_cinemas_changed = true;
242                         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
243                         Config::instance()->add_cinema(cinema);
244                 } catch (FileError& e) {
245                         error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file.  Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
246                         return;
247                 }
248
249                 wxTreeListItem previous = wxTLI_FIRST;
250                 bool found = false;
251                 auto search = wx_to_std(_search->GetValue());
252                 convert_to_lower(search);
253                 for (auto existing_cinema: cinemas) {
254                         if (!matches_search(existing_cinema, search)) {
255                                 continue;
256                         }
257                         if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
258                                 /* existing_cinema should be after the one we're inserting */
259                                 found = true;
260                                 break;
261                         }
262                         auto item = cinema_to_item(existing_cinema);
263                         DCPOMATIC_ASSERT(item);
264                         previous = *item;
265                 }
266
267                 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
268
269                 if (item) {
270                         _targets->UnselectAll ();
271                         _targets->Select (*item);
272                 }
273         }
274
275         selection_changed ();
276 }
277
278
279 shared_ptr<Cinema>
280 ScreensPanel::cinema_for_operation () const
281 {
282         if (_selected_cinemas.size() == 1) {
283                 return _selected_cinemas[0];
284         } else if (_selected_screens.size() == 1) {
285                 return _selected_screens[0]->cinema;
286         }
287
288         return {};
289 }
290
291
292 void
293 ScreensPanel::edit_cinema_clicked ()
294 {
295         auto cinema = cinema_for_operation ();
296         if (!cinema) {
297                 return;
298         }
299
300         CinemaDialog dialog(
301                 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
302                 );
303
304         if (dialog.ShowModal() == wxID_OK) {
305                 cinema->name = dialog.name();
306                 cinema->emails = dialog.emails();
307                 cinema->notes = dialog.notes();
308                 cinema->set_utc_offset_hour(dialog.utc_offset_hour());
309                 cinema->set_utc_offset_minute(dialog.utc_offset_minute());
310                 notify_cinemas_changed();
311                 auto item = cinema_to_item(cinema);
312                 DCPOMATIC_ASSERT(item);
313                 _targets->SetItemText (*item, std_to_wx(dialog.name()));
314         }
315 }
316
317
318 void
319 ScreensPanel::remove_cinema_clicked ()
320 {
321         if (_selected_cinemas.size() == 1) {
322                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
323                         return;
324                 }
325         } else {
326                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
327                         return;
328                 }
329         }
330
331         for (auto const& cinema: _selected_cinemas) {
332                 _ignore_cinemas_changed = true;
333                 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
334                 Config::instance()->remove_cinema(cinema);
335                 auto item = cinema_to_item(cinema);
336                 DCPOMATIC_ASSERT(item);
337                 _targets->DeleteItem(*item);
338         }
339
340         selection_changed ();
341 }
342
343
344 void
345 ScreensPanel::add_screen_clicked ()
346 {
347         auto cinema = cinema_for_operation ();
348         if (!cinema) {
349                 return;
350         }
351
352         ScreenDialog dialog(GetParent(), _("Add Screen"));
353
354         if (dialog.ShowModal () != wxID_OK) {
355                 return;
356         }
357
358         for (auto screen: cinema->screens()) {
359                 if (screen->name == dialog.name()) {
360                         error_dialog (
361                                 GetParent(),
362                                 wxString::Format (
363                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
364                                         std_to_wx(dialog.name()).data()
365                                         )
366                                 );
367                         return;
368                 }
369         }
370
371         auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
372         cinema->add_screen (screen);
373         notify_cinemas_changed();
374
375         auto id = add_screen (cinema, screen);
376         if (id) {
377                 _targets->Expand (id.get ());
378         }
379 }
380
381
382 void
383 ScreensPanel::edit_screen_clicked ()
384 {
385         if (_selected_screens.size() != 1) {
386                 return;
387         }
388
389         auto edit_screen = _selected_screens[0];
390
391         ScreenDialog dialog(
392                 GetParent(), _("Edit screen"),
393                 edit_screen->name,
394                 edit_screen->notes,
395                 edit_screen->recipient,
396                 edit_screen->recipient_file,
397                 edit_screen->trusted_devices
398                 );
399
400         if (dialog.ShowModal() != wxID_OK) {
401                 return;
402         }
403
404         auto cinema = edit_screen->cinema;
405         for (auto screen: cinema->screens()) {
406                 if (screen != edit_screen && screen->name == dialog.name()) {
407                         error_dialog (
408                                 GetParent(),
409                                 wxString::Format (
410                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
411                                         std_to_wx(dialog.name()).data()
412                                         )
413                                 );
414                         return;
415                 }
416         }
417
418         edit_screen->name = dialog.name();
419         edit_screen->notes = dialog.notes();
420         edit_screen->recipient = dialog.recipient();
421         edit_screen->recipient_file = dialog.recipient_file();
422         edit_screen->trusted_devices = dialog.trusted_devices();
423         notify_cinemas_changed();
424
425         auto item = screen_to_item(edit_screen);
426         DCPOMATIC_ASSERT (item);
427         _targets->SetItemText(*item, std_to_wx(dialog.name()));
428 }
429
430
431 void
432 ScreensPanel::remove_screen_clicked ()
433 {
434         if (_selected_screens.size() == 1) {
435                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
436                         return;
437                 }
438         } else {
439                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
440                         return;
441                 }
442         }
443
444         for (auto const& screen: _selected_screens) {
445                 screen->cinema->remove_screen(screen);
446                 auto item = screen_to_item(screen);
447                 DCPOMATIC_ASSERT(item);
448                 _targets->DeleteItem(*item);
449         }
450
451         /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
452          * as well.
453          */
454         selection_changed();
455         notify_cinemas_changed();
456 }
457
458
459 vector<shared_ptr<Screen>>
460 ScreensPanel::screens () const
461 {
462         vector<shared_ptr<Screen>> output;
463         std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
464         return output;
465 }
466
467
468 void
469 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
470 {
471         selection_changed ();
472 }
473
474
475 void
476 ScreensPanel::selection_changed ()
477 {
478         if (_ignore_selection_change) {
479                 return;
480         }
481
482         wxTreeListItems selection;
483         _targets->GetSelections (selection);
484
485         _selected_cinemas.clear ();
486         _selected_screens.clear ();
487
488         for (size_t i = 0; i < selection.size(); ++i) {
489                 if (auto cinema = item_to_cinema(selection[i])) {
490                         _selected_cinemas.push_back(cinema);
491                 }
492                 if (auto screen = item_to_screen(selection[i])) {
493                         _selected_screens.push_back(screen);
494                 }
495         }
496
497         setup_sensitivity ();
498 }
499
500
501 list<shared_ptr<Cinema>>
502 ScreensPanel::sorted_cinemas() const
503 {
504         auto cinemas = Config::instance()->cinemas();
505
506         cinemas.sort(
507                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
508                 );
509
510         return cinemas;
511 }
512
513
514 void
515 ScreensPanel::add_cinemas ()
516 {
517         for (auto cinema: sorted_cinemas()) {
518                 add_cinema (cinema, wxTLI_LAST);
519         }
520 }
521
522
523 void
524 ScreensPanel::clear_and_re_add()
525 {
526         _targets->DeleteAllItems ();
527
528         _item_to_cinema.clear ();
529         _cinema_to_item.clear ();
530         _item_to_screen.clear ();
531         _screen_to_item.clear ();
532
533         add_cinemas ();
534 }
535
536
537 void
538 ScreensPanel::search_changed ()
539 {
540         clear_and_re_add();
541
542         _ignore_selection_change = true;
543
544         for (auto const& selection: _selected_cinemas) {
545                 if (auto item = cinema_to_item(selection)) {
546                         _targets->Select (*item);
547                 }
548         }
549
550         for (auto const& selection: _selected_screens) {
551                 if (auto item = screen_to_item(selection)) {
552                         _targets->Select (*item);
553                 }
554         }
555
556         _ignore_selection_change = false;
557
558         _ignore_check_change = true;
559
560         for (auto const& checked: _checked_screens) {
561                 if (auto item = screen_to_item(checked)) {
562                         _targets->CheckItem(*item, wxCHK_CHECKED);
563                         setup_cinema_checked_state(*item);
564                 }
565         }
566
567         _ignore_check_change = false;
568 }
569
570
571 void
572 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
573 {
574         auto screen = item_to_screen(item);
575         DCPOMATIC_ASSERT(screen);
576         if (checked) {
577                 _checked_screens.insert(screen);
578         } else {
579                 _checked_screens.erase(screen);
580         }
581 }
582
583
584 void
585 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
586 {
587         auto cinema = _targets->GetItemParent(screen);
588         DCPOMATIC_ASSERT (cinema.IsOk());
589         int checked = 0;
590         int unchecked = 0;
591         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
592                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
593                     ++checked;
594                 } else {
595                     ++unchecked;
596                 }
597         }
598         if (checked == 0) {
599                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
600         } else if (unchecked == 0) {
601                 _targets->CheckItem(cinema, wxCHK_CHECKED);
602         } else {
603                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
604         }
605 }
606
607
608 void
609 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
610 {
611         if (_ignore_check_change) {
612                 return;
613         }
614
615         if (item_to_cinema(ev.GetItem())) {
616                 /* Cinema: check/uncheck all children */
617                 auto const checked = _targets->GetCheckedState(ev.GetItem());
618                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
619                         _targets->CheckItem(child, checked);
620                         set_screen_checked(child, checked);
621                 }
622         } else {
623                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
624                 setup_cinema_checked_state(ev.GetItem());
625         }
626
627         ScreensChanged ();
628 }
629
630
631 shared_ptr<Cinema>
632 ScreensPanel::item_to_cinema (wxTreeListItem item) const
633 {
634         auto iter = _item_to_cinema.find (item);
635         if (iter == _item_to_cinema.end()) {
636                 return {};
637         }
638
639         return iter->second;
640 }
641
642
643 shared_ptr<Screen>
644 ScreensPanel::item_to_screen (wxTreeListItem item) const
645 {
646         auto iter = _item_to_screen.find (item);
647         if (iter == _item_to_screen.end()) {
648                 return {};
649         }
650
651         return iter->second;
652 }
653
654
655 optional<wxTreeListItem>
656 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
657 {
658         auto iter = _cinema_to_item.find (cinema);
659         if (iter == _cinema_to_item.end()) {
660                 return {};
661         }
662
663         return iter->second;
664 }
665
666
667 optional<wxTreeListItem>
668 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
669 {
670         auto iter = _screen_to_item.find (screen);
671         if (iter == _screen_to_item.end()) {
672                 return {};
673         }
674
675         return iter->second;
676 }
677
678
679 bool
680 ScreensPanel::notify_cinemas_changed()
681 {
682         _ignore_cinemas_changed = true;
683         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
684
685         try {
686                 Config::instance()->changed(Config::CINEMAS);
687         } catch (FileError& e) {
688                 error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file.  Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
689                 return false;
690         }
691
692         return true;
693 }
694
695
696 void
697 ScreensPanel::config_changed(Config::Property property)
698 {
699         if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
700                 clear_and_re_add();
701         }
702 }