272b983d90c8bf5e72c81e00c3b38ef3e9791163
[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::make_pair;
36 using std::make_shared;
37 using std::map;
38 using std::pair;
39 using std::shared_ptr;
40 using std::string;
41 using std::vector;
42 using boost::optional;
43 #if BOOST_VERSION >= 106100
44 using namespace boost::placeholders;
45 #endif
46 using namespace dcpomatic;
47
48
49 ScreensPanel::ScreensPanel (wxWindow* parent)
50         : wxPanel (parent, wxID_ANY)
51 {
52         auto sizer = new wxBoxSizer (wxVERTICAL);
53
54         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
55 #ifndef __WXGTK3__
56         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
57         _search->ShowCancelButton (true);
58 #endif
59         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
60
61         auto targets = new wxBoxSizer (wxHORIZONTAL);
62         _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
63         _targets->AppendColumn (wxT("foo"));
64
65         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
66
67         add_cinemas ();
68
69         auto side_buttons = new wxBoxSizer (wxVERTICAL);
70
71         auto target_buttons = new wxBoxSizer (wxVERTICAL);
72
73         _add_cinema = new Button (this, _("Add Cinema..."));
74         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
75         _edit_cinema = new Button (this, _("Edit Cinema..."));
76         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77         _remove_cinema = new Button (this, _("Remove Cinema"));
78         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79         _add_screen = new Button (this, _("Add Screen..."));
80         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81         _edit_screen = new Button (this, _("Edit Screen..."));
82         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
83         _remove_screen = new Button (this, _("Remove Screen"));
84         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
85
86         side_buttons->Add (target_buttons, 0, 0);
87
88         auto check_buttons = new wxBoxSizer (wxVERTICAL);
89
90         _check_all = new Button (this, _("Check all"));
91         check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
92         _uncheck_all = new Button (this, _("Uncheck all"));
93         check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
94
95         side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
96
97         targets->Add (side_buttons, 0, 0);
98
99         sizer->Add (targets, 1, wxEXPAND);
100
101         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
102         _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
103         _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
104
105         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
106         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
107         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
108
109         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
110         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
111         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
112
113         _check_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
114         _uncheck_all->Bind   (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
115
116         SetSizer (sizer);
117
118         _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
119 }
120
121
122 ScreensPanel::~ScreensPanel ()
123 {
124         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
125         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
126 }
127
128
129 void
130 ScreensPanel::check_all ()
131 {
132         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
133                 _targets->CheckItem(cinema, wxCHK_CHECKED);
134                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
135                         _targets->CheckItem(screen, wxCHK_CHECKED);
136                         set_screen_checked(screen, true);
137                 }
138         }
139 }
140
141
142 void
143 ScreensPanel::uncheck_all ()
144 {
145         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
146                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
147                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
148                         _targets->CheckItem(screen, wxCHK_UNCHECKED);
149                         set_screen_checked(screen, false);
150                 }
151         }
152 }
153
154
155 void
156 ScreensPanel::setup_sensitivity ()
157 {
158         bool const sc = _selected_cinemas.size() == 1;
159         bool const ss = _selected_screens.size() == 1;
160
161         _edit_cinema->Enable (sc || ss);
162         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
163
164         _add_screen->Enable (sc || ss);
165         _edit_screen->Enable (ss);
166         _remove_screen->Enable (_selected_screens.size() >= 1);
167 }
168
169
170 void
171 ScreensPanel::convert_to_lower(string& s)
172 {
173         transform(s.begin(), s.end(), s.begin(), ::tolower);
174 }
175
176
177 bool
178 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string lower_case_search)
179 {
180         if (lower_case_search.empty()) {
181                 return true;
182         }
183
184         auto name = cinema->name;
185         convert_to_lower(name);
186         return name.find(lower_case_search) != string::npos;
187 }
188
189
190 optional<wxTreeListItem>
191 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
192 {
193         auto search = wx_to_std (_search->GetValue ());
194         convert_to_lower(search);
195         if (!matches_search(cinema, search)) {
196                 return {};
197         }
198
199         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
200
201         _item_to_cinema[id] = cinema;
202         _cinema_to_item[cinema] = id;
203
204         for (auto screen: cinema->screens()) {
205                 add_screen (cinema, screen);
206         }
207
208         return id;
209 }
210
211
212 optional<wxTreeListItem>
213 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
214 {
215         auto item = cinema_to_item(cinema);
216         if (!item) {
217                 return {};
218         }
219
220         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
221
222         _item_to_screen[id] = screen;
223         _screen_to_item[screen] = id;
224
225         return item;
226 }
227
228
229 void
230 ScreensPanel::add_cinema_clicked ()
231 {
232         auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
233         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
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 = Config::instance()->cinemas();
239                 cinemas.sort(
240                         [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
241                         );
242
243                 try {
244                         _ignore_cinemas_changed = true;
245                         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
246                         Config::instance()->add_cinema(cinema);
247                 } catch (FileError& e) {
248                         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()));
249                         return;
250                 }
251
252                 wxTreeListItem previous = wxTLI_FIRST;
253                 bool found = false;
254                 for (auto existing_cinema: cinemas) {
255                         if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
256                                 /* existing_cinema should be after the one we're inserting */
257                                 found = true;
258                                 break;
259                         }
260                         auto item = cinema_to_item(existing_cinema);
261                         DCPOMATIC_ASSERT(item);
262                         previous = *item;
263                 }
264
265                 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
266
267                 if (item) {
268                         _targets->UnselectAll ();
269                         _targets->Select (*item);
270                 }
271         }
272
273         selection_changed ();
274 }
275
276
277 shared_ptr<Cinema>
278 ScreensPanel::cinema_for_operation () const
279 {
280         if (_selected_cinemas.size() == 1) {
281                 return _selected_cinemas[0];
282         } else if (_selected_screens.size() == 1) {
283                 return _selected_screens[0]->cinema;
284         }
285
286         return {};
287 }
288
289
290 void
291 ScreensPanel::edit_cinema_clicked ()
292 {
293         auto cinema = cinema_for_operation ();
294         if (!cinema) {
295                 return;
296         }
297
298         auto dialog = new CinemaDialog(
299                 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
300                 );
301         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
302
303         if (dialog->ShowModal() == wxID_OK) {
304                 cinema->name = dialog->name();
305                 cinema->emails = dialog->emails();
306                 cinema->notes = dialog->notes();
307                 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
308                 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
309                 notify_cinemas_changed();
310                 auto item = cinema_to_item(cinema);
311                 DCPOMATIC_ASSERT(item);
312                 _targets->SetItemText (*item, std_to_wx(dialog->name()));
313         }
314 }
315
316
317 void
318 ScreensPanel::remove_cinema_clicked ()
319 {
320         if (_selected_cinemas.size() == 1) {
321                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
322                         return;
323                 }
324         } else {
325                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
326                         return;
327                 }
328         }
329
330         for (auto const& cinema: _selected_cinemas) {
331                 _ignore_cinemas_changed = true;
332                 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
333                 Config::instance()->remove_cinema(cinema);
334                 auto item = cinema_to_item(cinema);
335                 DCPOMATIC_ASSERT(item);
336                 _targets->DeleteItem(*item);
337         }
338
339         selection_changed ();
340 }
341
342
343 void
344 ScreensPanel::add_screen_clicked ()
345 {
346         auto cinema = cinema_for_operation ();
347         if (!cinema) {
348                 return;
349         }
350
351         auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
352         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
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         auto dialog = new ScreenDialog(
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         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
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 void
499 ScreensPanel::add_cinemas ()
500 {
501         auto cinemas = Config::instance()->cinemas();
502         cinemas.sort(
503                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
504                 );
505
506         for (auto cinema: cinemas) {
507                 add_cinema (cinema, wxTLI_LAST);
508         }
509 }
510
511
512 void
513 ScreensPanel::clear_and_re_add()
514 {
515         _targets->DeleteAllItems ();
516
517         _item_to_cinema.clear ();
518         _cinema_to_item.clear ();
519         _item_to_screen.clear ();
520         _screen_to_item.clear ();
521
522         add_cinemas ();
523 }
524
525
526 void
527 ScreensPanel::search_changed ()
528 {
529         clear_and_re_add();
530
531         _ignore_selection_change = true;
532
533         for (auto const& selection: _selected_cinemas) {
534                 if (auto item = cinema_to_item(selection)) {
535                         _targets->Select (*item);
536                 }
537         }
538
539         for (auto const& selection: _selected_screens) {
540                 if (auto item = screen_to_item(selection)) {
541                         _targets->Select (*item);
542                 }
543         }
544
545         _ignore_selection_change = false;
546
547         _ignore_check_change = true;
548
549         for (auto const& checked: _checked_screens) {
550                 if (auto item = screen_to_item(checked)) {
551                         _targets->CheckItem(*item, wxCHK_CHECKED);
552                         setup_cinema_checked_state(*item);
553                 }
554         }
555
556         _ignore_check_change = false;
557 }
558
559
560 void
561 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
562 {
563         auto screen = item_to_screen(item);
564         DCPOMATIC_ASSERT(screen);
565         if (checked) {
566                 _checked_screens.insert(screen);
567         } else {
568                 _checked_screens.erase(screen);
569         }
570 }
571
572
573 void
574 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
575 {
576         auto cinema = _targets->GetItemParent(screen);
577         DCPOMATIC_ASSERT (cinema.IsOk());
578         int checked = 0;
579         int unchecked = 0;
580         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
581                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
582                     ++checked;
583                 } else {
584                     ++unchecked;
585                 }
586         }
587         if (checked == 0) {
588                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
589         } else if (unchecked == 0) {
590                 _targets->CheckItem(cinema, wxCHK_CHECKED);
591         } else {
592                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
593         }
594 }
595
596
597 void
598 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
599 {
600         if (_ignore_check_change) {
601                 return;
602         }
603
604         if (item_to_cinema(ev.GetItem())) {
605                 /* Cinema: check/uncheck all children */
606                 auto const checked = _targets->GetCheckedState(ev.GetItem());
607                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
608                         _targets->CheckItem(child, checked);
609                         set_screen_checked(child, checked);
610                 }
611         } else {
612                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
613                 setup_cinema_checked_state(ev.GetItem());
614         }
615
616         ScreensChanged ();
617 }
618
619
620 shared_ptr<Cinema>
621 ScreensPanel::item_to_cinema (wxTreeListItem item) const
622 {
623         auto iter = _item_to_cinema.find (item);
624         if (iter == _item_to_cinema.end()) {
625                 return {};
626         }
627
628         return iter->second;
629 }
630
631
632 shared_ptr<Screen>
633 ScreensPanel::item_to_screen (wxTreeListItem item) const
634 {
635         auto iter = _item_to_screen.find (item);
636         if (iter == _item_to_screen.end()) {
637                 return {};
638         }
639
640         return iter->second;
641 }
642
643
644 optional<wxTreeListItem>
645 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
646 {
647         auto iter = _cinema_to_item.find (cinema);
648         if (iter == _cinema_to_item.end()) {
649                 return {};
650         }
651
652         return iter->second;
653 }
654
655
656 optional<wxTreeListItem>
657 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
658 {
659         auto iter = _screen_to_item.find (screen);
660         if (iter == _screen_to_item.end()) {
661                 return {};
662         }
663
664         return iter->second;
665 }
666
667
668 bool
669 ScreensPanel::notify_cinemas_changed()
670 {
671         _ignore_cinemas_changed = true;
672         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
673
674         try {
675                 Config::instance()->changed(Config::CINEMAS);
676         } catch (FileError& e) {
677                 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()));
678                 return false;
679         }
680
681         return true;
682 }
683
684
685 void
686 ScreensPanel::config_changed(Config::Property property)
687 {
688         if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
689                 clear_and_re_add();
690         }
691 }