a726030ade2049e34a3575e9398acde44ee2a2f7
[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 optional<wxTreeListItem>
171 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
172 {
173         auto search = wx_to_std (_search->GetValue ());
174         transform (search.begin(), search.end(), search.begin(), ::tolower);
175
176         if (!search.empty ()) {
177                 auto name = cinema->name;
178                 transform (name.begin(), name.end(), name.begin(), ::tolower);
179                 if (name.find (search) == string::npos) {
180                         return {};
181                 }
182         }
183
184         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
185
186         _item_to_cinema[id] = cinema;
187         _cinema_to_item[cinema] = id;
188
189         for (auto screen: cinema->screens()) {
190                 add_screen (cinema, screen);
191         }
192
193         return id;
194 }
195
196
197 optional<wxTreeListItem>
198 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
199 {
200         auto item = cinema_to_item(cinema);
201         if (!item) {
202                 return {};
203         }
204
205         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
206
207         _item_to_screen[id] = screen;
208         _screen_to_item[screen] = id;
209
210         return item;
211 }
212
213
214 void
215 ScreensPanel::add_cinema_clicked ()
216 {
217         auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
218         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
219
220         if (dialog->ShowModal() == wxID_OK) {
221                 auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
222
223                 auto cinemas = Config::instance()->cinemas();
224                 cinemas.sort(
225                         [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
226                         );
227
228                 try {
229                         _ignore_cinemas_changed = true;
230                         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
231                         Config::instance()->add_cinema(cinema);
232                 } catch (FileError& e) {
233                         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()));
234                         return;
235                 }
236
237                 wxTreeListItem previous = wxTLI_FIRST;
238                 bool found = false;
239                 for (auto existing_cinema: cinemas) {
240                         if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
241                                 /* existing_cinema should be after the one we're inserting */
242                                 found = true;
243                                 break;
244                         }
245                         auto item = cinema_to_item(existing_cinema);
246                         DCPOMATIC_ASSERT(item);
247                         previous = *item;
248                 }
249
250                 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
251
252                 if (item) {
253                         _targets->UnselectAll ();
254                         _targets->Select (*item);
255                 }
256         }
257
258         selection_changed ();
259 }
260
261
262 shared_ptr<Cinema>
263 ScreensPanel::cinema_for_operation () const
264 {
265         if (_selected_cinemas.size() == 1) {
266                 return _selected_cinemas[0];
267         } else if (_selected_screens.size() == 1) {
268                 return _selected_screens[0]->cinema;
269         }
270
271         return {};
272 }
273
274
275 void
276 ScreensPanel::edit_cinema_clicked ()
277 {
278         auto cinema = cinema_for_operation ();
279         if (!cinema) {
280                 return;
281         }
282
283         auto dialog = new CinemaDialog(
284                 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
285                 );
286         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
287
288         if (dialog->ShowModal() == wxID_OK) {
289                 cinema->name = dialog->name();
290                 cinema->emails = dialog->emails();
291                 cinema->notes = dialog->notes();
292                 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
293                 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
294                 notify_cinemas_changed();
295                 auto item = cinema_to_item(cinema);
296                 DCPOMATIC_ASSERT(item);
297                 _targets->SetItemText (*item, std_to_wx(dialog->name()));
298         }
299 }
300
301
302 void
303 ScreensPanel::remove_cinema_clicked ()
304 {
305         if (_selected_cinemas.size() == 1) {
306                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
307                         return;
308                 }
309         } else {
310                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
311                         return;
312                 }
313         }
314
315         for (auto const& cinema: _selected_cinemas) {
316                 _ignore_cinemas_changed = true;
317                 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
318                 Config::instance()->remove_cinema(cinema);
319                 auto item = cinema_to_item(cinema);
320                 DCPOMATIC_ASSERT(item);
321                 _targets->DeleteItem(*item);
322         }
323
324         selection_changed ();
325 }
326
327
328 void
329 ScreensPanel::add_screen_clicked ()
330 {
331         auto cinema = cinema_for_operation ();
332         if (!cinema) {
333                 return;
334         }
335
336         auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
337         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
338
339         if (dialog->ShowModal () != wxID_OK) {
340                 return;
341         }
342
343         for (auto screen: cinema->screens()) {
344                 if (screen->name == dialog->name()) {
345                         error_dialog (
346                                 GetParent(),
347                                 wxString::Format (
348                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
349                                         std_to_wx(dialog->name()).data()
350                                         )
351                                 );
352                         return;
353                 }
354         }
355
356         auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
357         cinema->add_screen (screen);
358         notify_cinemas_changed();
359
360         auto id = add_screen (cinema, screen);
361         if (id) {
362                 _targets->Expand (id.get ());
363         }
364 }
365
366
367 void
368 ScreensPanel::edit_screen_clicked ()
369 {
370         if (_selected_screens.size() != 1) {
371                 return;
372         }
373
374         auto edit_screen = _selected_screens[0];
375
376         auto dialog = new ScreenDialog(
377                 GetParent(), _("Edit screen"),
378                 edit_screen->name,
379                 edit_screen->notes,
380                 edit_screen->recipient,
381                 edit_screen->recipient_file,
382                 edit_screen->trusted_devices
383                 );
384         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
385
386         if (dialog->ShowModal() != wxID_OK) {
387                 return;
388         }
389
390         auto cinema = edit_screen->cinema;
391         for (auto screen: cinema->screens()) {
392                 if (screen != edit_screen && screen->name == dialog->name()) {
393                         error_dialog (
394                                 GetParent(),
395                                 wxString::Format (
396                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
397                                         std_to_wx(dialog->name()).data()
398                                         )
399                                 );
400                         return;
401                 }
402         }
403
404         edit_screen->name = dialog->name();
405         edit_screen->notes = dialog->notes();
406         edit_screen->recipient = dialog->recipient();
407         edit_screen->recipient_file = dialog->recipient_file();
408         edit_screen->trusted_devices = dialog->trusted_devices();
409         notify_cinemas_changed();
410
411         auto item = screen_to_item(edit_screen);
412         DCPOMATIC_ASSERT (item);
413         _targets->SetItemText (*item, std_to_wx(dialog->name()));
414 }
415
416
417 void
418 ScreensPanel::remove_screen_clicked ()
419 {
420         if (_selected_screens.size() == 1) {
421                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
422                         return;
423                 }
424         } else {
425                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
426                         return;
427                 }
428         }
429
430         for (auto const& screen: _selected_screens) {
431                 screen->cinema->remove_screen(screen);
432                 auto item = screen_to_item(screen);
433                 DCPOMATIC_ASSERT(item);
434                 _targets->DeleteItem(*item);
435         }
436
437         notify_cinemas_changed();
438 }
439
440
441 vector<shared_ptr<Screen>>
442 ScreensPanel::screens () const
443 {
444         vector<shared_ptr<Screen>> output;
445         std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
446         return output;
447 }
448
449
450 void
451 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
452 {
453         selection_changed ();
454 }
455
456
457 void
458 ScreensPanel::selection_changed ()
459 {
460         if (_ignore_selection_change) {
461                 return;
462         }
463
464         wxTreeListItems selection;
465         _targets->GetSelections (selection);
466
467         _selected_cinemas.clear ();
468         _selected_screens.clear ();
469
470         for (size_t i = 0; i < selection.size(); ++i) {
471                 if (auto cinema = item_to_cinema(selection[i])) {
472                         _selected_cinemas.push_back(cinema);
473                 }
474                 if (auto screen = item_to_screen(selection[i])) {
475                         _selected_screens.push_back(screen);
476                 }
477         }
478
479         setup_sensitivity ();
480 }
481
482
483 void
484 ScreensPanel::add_cinemas ()
485 {
486         auto cinemas = Config::instance()->cinemas();
487         cinemas.sort(
488                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
489                 );
490
491         for (auto cinema: cinemas) {
492                 add_cinema (cinema, wxTLI_LAST);
493         }
494 }
495
496
497 void
498 ScreensPanel::clear_and_re_add()
499 {
500         _targets->DeleteAllItems ();
501
502         _item_to_cinema.clear ();
503         _cinema_to_item.clear ();
504         _item_to_screen.clear ();
505         _screen_to_item.clear ();
506
507         add_cinemas ();
508 }
509
510
511 void
512 ScreensPanel::search_changed ()
513 {
514         clear_and_re_add();
515
516         _ignore_selection_change = true;
517
518         for (auto const& selection: _selected_cinemas) {
519                 if (auto item = cinema_to_item(selection)) {
520                         _targets->Select (*item);
521                 }
522         }
523
524         for (auto const& selection: _selected_screens) {
525                 if (auto item = screen_to_item(selection)) {
526                         _targets->Select (*item);
527                 }
528         }
529
530         _ignore_selection_change = false;
531
532         _ignore_check_change = true;
533
534         for (auto const& checked: _checked_screens) {
535                 if (auto item = screen_to_item(checked)) {
536                         _targets->CheckItem(*item, wxCHK_CHECKED);
537                         setup_cinema_checked_state(*item);
538                 }
539         }
540
541         _ignore_check_change = false;
542 }
543
544
545 void
546 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
547 {
548         auto screen = item_to_screen(item);
549         DCPOMATIC_ASSERT(screen);
550         if (checked) {
551                 _checked_screens.insert(screen);
552         } else {
553                 _checked_screens.erase(screen);
554         }
555 }
556
557
558 void
559 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
560 {
561         auto cinema = _targets->GetItemParent(screen);
562         DCPOMATIC_ASSERT (cinema.IsOk());
563         int checked = 0;
564         int unchecked = 0;
565         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
566                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
567                     ++checked;
568                 } else {
569                     ++unchecked;
570                 }
571         }
572         if (checked == 0) {
573                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
574         } else if (unchecked == 0) {
575                 _targets->CheckItem(cinema, wxCHK_CHECKED);
576         } else {
577                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
578         }
579 }
580
581
582 void
583 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
584 {
585         if (_ignore_check_change) {
586                 return;
587         }
588
589         if (item_to_cinema(ev.GetItem())) {
590                 /* Cinema: check/uncheck all children */
591                 auto const checked = _targets->GetCheckedState(ev.GetItem());
592                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
593                         _targets->CheckItem(child, checked);
594                         set_screen_checked(child, checked);
595                 }
596         } else {
597                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
598                 setup_cinema_checked_state(ev.GetItem());
599         }
600
601         ScreensChanged ();
602 }
603
604
605 shared_ptr<Cinema>
606 ScreensPanel::item_to_cinema (wxTreeListItem item) const
607 {
608         auto iter = _item_to_cinema.find (item);
609         if (iter == _item_to_cinema.end()) {
610                 return {};
611         }
612
613         return iter->second;
614 }
615
616
617 shared_ptr<Screen>
618 ScreensPanel::item_to_screen (wxTreeListItem item) const
619 {
620         auto iter = _item_to_screen.find (item);
621         if (iter == _item_to_screen.end()) {
622                 return {};
623         }
624
625         return iter->second;
626 }
627
628
629 optional<wxTreeListItem>
630 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
631 {
632         auto iter = _cinema_to_item.find (cinema);
633         if (iter == _cinema_to_item.end()) {
634                 return {};
635         }
636
637         return iter->second;
638 }
639
640
641 optional<wxTreeListItem>
642 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
643 {
644         auto iter = _screen_to_item.find (screen);
645         if (iter == _screen_to_item.end()) {
646                 return {};
647         }
648
649         return iter->second;
650 }
651
652
653 bool
654 ScreensPanel::notify_cinemas_changed()
655 {
656         _ignore_cinemas_changed = true;
657         ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
658
659         try {
660                 Config::instance()->changed(Config::CINEMAS);
661         } catch (FileError& e) {
662                 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()));
663                 return false;
664         }
665
666         return true;
667 }
668
669
670 void
671 ScreensPanel::config_changed(Config::Property property)
672 {
673         if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
674                 clear_and_re_add();
675         }
676 }