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