Cleanup: use some better variable names and ScopeGuards.
[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 #include <unicode/putil.h>
33 #include <unicode/ucol.h>
34 #include <unicode/uiter.h>
35 #include <unicode/utypes.h>
36 #include <unicode/ustring.h>
37
38
39 using std::cout;
40 using std::make_pair;
41 using std::make_shared;
42 using std::map;
43 using std::pair;
44 using std::shared_ptr;
45 using std::string;
46 using std::vector;
47 using boost::optional;
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         UErrorCode status = U_ZERO_ERROR;
121         _collator = ucol_open(nullptr, &status);
122         if (_collator) {
123                 ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
124                 ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
125                 ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
126         }
127 }
128
129
130 ScreensPanel::~ScreensPanel ()
131 {
132         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
133         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
134
135         if (_collator) {
136                 ucol_close (_collator);
137         }
138 }
139
140
141 void
142 ScreensPanel::check_all ()
143 {
144         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
145                 _targets->CheckItem(cinema, wxCHK_CHECKED);
146                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
147                         _targets->CheckItem(screen, wxCHK_CHECKED);
148                         set_screen_checked(screen, true);
149                 }
150         }
151 }
152
153
154 void
155 ScreensPanel::uncheck_all ()
156 {
157         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
158                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
159                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
160                         _targets->CheckItem(screen, wxCHK_UNCHECKED);
161                         set_screen_checked(screen, false);
162                 }
163         }
164 }
165
166
167 void
168 ScreensPanel::setup_sensitivity ()
169 {
170         bool const sc = _selected_cinemas.size() == 1;
171         bool const ss = _selected_screens.size() == 1;
172
173         _edit_cinema->Enable (sc || ss);
174         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
175
176         _add_screen->Enable (sc || ss);
177         _edit_screen->Enable (ss);
178         _remove_screen->Enable (_selected_screens.size() >= 1);
179 }
180
181
182 optional<wxTreeListItem>
183 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
184 {
185         auto search = wx_to_std (_search->GetValue ());
186         transform (search.begin(), search.end(), search.begin(), ::tolower);
187
188         if (!search.empty ()) {
189                 auto name = cinema->name;
190                 transform (name.begin(), name.end(), name.begin(), ::tolower);
191                 if (name.find (search) == string::npos) {
192                         return {};
193                 }
194         }
195
196         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
197
198         _item_to_cinema[id] = cinema;
199         _cinema_to_item[cinema] = id;
200
201         for (auto screen: cinema->screens()) {
202                 add_screen (cinema, screen);
203         }
204
205         return id;
206 }
207
208
209 optional<wxTreeListItem>
210 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
211 {
212         auto item = cinema_to_item(cinema);
213         if (!item) {
214                 return {};
215         }
216
217         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
218
219         _item_to_screen[id] = screen;
220         _screen_to_item[screen] = id;
221
222         return item;
223 }
224
225
226 void
227 ScreensPanel::add_cinema_clicked ()
228 {
229         auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
230         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
231
232         if (dialog->ShowModal() == wxID_OK) {
233                 auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
234
235                 auto cinemas = Config::instance()->cinemas();
236                 cinemas.sort(
237                         [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
238                         );
239
240                 optional<wxTreeListItem> item;
241                 for (auto existing_cinema: cinemas) {
242                         if (!item && compare(dialog->name(), existing_cinema->name) < 0) {
243                                 if (auto existing_item = cinema_to_item(existing_cinema)) {
244                                         item = add_cinema (cinema, *existing_item);
245                                 }
246                         }
247                 }
248
249                 if (!item) {
250                         item = add_cinema (cinema, wxTLI_LAST);
251                 }
252
253                 if (item) {
254                         _targets->UnselectAll ();
255                         _targets->Select (*item);
256                 }
257
258                 Config::instance()->add_cinema (cinema);
259         }
260
261         selection_changed ();
262 }
263
264
265 shared_ptr<Cinema>
266 ScreensPanel::cinema_for_operation () const
267 {
268         if (_selected_cinemas.size() == 1) {
269                 return _selected_cinemas[0];
270         } else if (_selected_screens.size() == 1) {
271                 return _selected_screens[0]->cinema;
272         }
273
274         return {};
275 }
276
277
278 void
279 ScreensPanel::edit_cinema_clicked ()
280 {
281         auto cinema = cinema_for_operation ();
282         if (!cinema) {
283                 return;
284         }
285
286         auto dialog = new CinemaDialog(
287                 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
288                 );
289         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
290
291         if (dialog->ShowModal() == wxID_OK) {
292                 cinema->name = dialog->name();
293                 cinema->emails = dialog->emails();
294                 cinema->notes = dialog->notes();
295                 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
296                 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
297                 auto item = cinema_to_item(cinema);
298                 DCPOMATIC_ASSERT(item);
299                 _targets->SetItemText (*item, std_to_wx(dialog->name()));
300                 Config::instance()->changed (Config::CINEMAS);
301         }
302 }
303
304
305 void
306 ScreensPanel::remove_cinema_clicked ()
307 {
308         if (_selected_cinemas.size() == 1) {
309                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
310                         return;
311                 }
312         } else {
313                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
314                         return;
315                 }
316         }
317
318         for (auto const& cinema: _selected_cinemas) {
319                 Config::instance()->remove_cinema(cinema);
320                 auto item = cinema_to_item(cinema);
321                 DCPOMATIC_ASSERT(item);
322                 _targets->DeleteItem(*item);
323         }
324
325         selection_changed ();
326 }
327
328
329 void
330 ScreensPanel::add_screen_clicked ()
331 {
332         auto cinema = cinema_for_operation ();
333         if (!cinema) {
334                 return;
335         }
336
337         auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
338         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
339
340         if (dialog->ShowModal () != wxID_OK) {
341                 return;
342         }
343
344         for (auto screen: cinema->screens()) {
345                 if (screen->name == dialog->name()) {
346                         error_dialog (
347                                 GetParent(),
348                                 wxString::Format (
349                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
350                                         std_to_wx(dialog->name()).data()
351                                         )
352                                 );
353                         return;
354                 }
355         }
356
357         auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
358         cinema->add_screen (screen);
359         auto id = add_screen (cinema, screen);
360         if (id) {
361                 _targets->Expand (id.get ());
362         }
363
364         Config::instance()->changed (Config::CINEMAS);
365 }
366
367
368 void
369 ScreensPanel::edit_screen_clicked ()
370 {
371         if (_selected_screens.size() != 1) {
372                 return;
373         }
374
375         auto edit_screen = _selected_screens[0];
376
377         auto dialog = new ScreenDialog(
378                 GetParent(), _("Edit screen"),
379                 edit_screen->name,
380                 edit_screen->notes,
381                 edit_screen->recipient,
382                 edit_screen->recipient_file,
383                 edit_screen->trusted_devices
384                 );
385         ScopeGuard sg = [dialog]() { dialog->Destroy(); };
386
387         if (dialog->ShowModal() != wxID_OK) {
388                 return;
389         }
390
391         auto cinema = edit_screen->cinema;
392         for (auto screen: cinema->screens()) {
393                 if (screen != edit_screen && screen->name == dialog->name()) {
394                         error_dialog (
395                                 GetParent(),
396                                 wxString::Format (
397                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
398                                         std_to_wx(dialog->name()).data()
399                                         )
400                                 );
401                         return;
402                 }
403         }
404
405         edit_screen->name = dialog->name();
406         edit_screen->notes = dialog->notes();
407         edit_screen->recipient = dialog->recipient();
408         edit_screen->recipient_file = dialog->recipient_file();
409         edit_screen->trusted_devices = dialog->trusted_devices();
410         auto item = screen_to_item(edit_screen);
411         DCPOMATIC_ASSERT (item);
412         _targets->SetItemText (*item, std_to_wx(dialog->name()));
413         Config::instance()->changed (Config::CINEMAS);
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         Config::instance()->changed (Config::CINEMAS);
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 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::search_changed ()
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         _ignore_selection_change = true;
510
511         for (auto const& selection: _selected_cinemas) {
512                 if (auto item = cinema_to_item(selection)) {
513                         _targets->Select (*item);
514                 }
515         }
516
517         for (auto const& selection: _selected_screens) {
518                 if (auto item = screen_to_item(selection)) {
519                         _targets->Select (*item);
520                 }
521         }
522
523         _ignore_selection_change = false;
524
525         _ignore_check_change = true;
526
527         for (auto const& checked: _checked_screens) {
528                 if (auto item = screen_to_item(checked)) {
529                         _targets->CheckItem(*item, wxCHK_CHECKED);
530                         setup_cinema_checked_state(*item);
531                 }
532         }
533
534         _ignore_check_change = false;
535 }
536
537
538 void
539 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
540 {
541         auto screen = item_to_screen(item);
542         DCPOMATIC_ASSERT(screen);
543         if (checked) {
544                 _checked_screens.insert(screen);
545         } else {
546                 _checked_screens.erase(screen);
547         }
548 }
549
550
551 void
552 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
553 {
554         auto cinema = _targets->GetItemParent(screen);
555         DCPOMATIC_ASSERT (cinema.IsOk());
556         int checked = 0;
557         int unchecked = 0;
558         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
559                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
560                     ++checked;
561                 } else {
562                     ++unchecked;
563                 }
564         }
565         if (checked == 0) {
566                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
567         } else if (unchecked == 0) {
568                 _targets->CheckItem(cinema, wxCHK_CHECKED);
569         } else {
570                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
571         }
572 }
573
574
575 void
576 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
577 {
578         if (_ignore_check_change) {
579                 return;
580         }
581
582         if (item_to_cinema(ev.GetItem())) {
583                 /* Cinema: check/uncheck all children */
584                 auto const checked = _targets->GetCheckedState(ev.GetItem());
585                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
586                         _targets->CheckItem(child, checked);
587                         set_screen_checked(child, checked);
588                 }
589         } else {
590                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
591                 setup_cinema_checked_state(ev.GetItem());
592         }
593
594         ScreensChanged ();
595 }
596
597
598 shared_ptr<Cinema>
599 ScreensPanel::item_to_cinema (wxTreeListItem item) const
600 {
601         auto iter = _item_to_cinema.find (item);
602         if (iter == _item_to_cinema.end()) {
603                 return {};
604         }
605
606         return iter->second;
607 }
608
609
610 shared_ptr<Screen>
611 ScreensPanel::item_to_screen (wxTreeListItem item) const
612 {
613         auto iter = _item_to_screen.find (item);
614         if (iter == _item_to_screen.end()) {
615                 return {};
616         }
617
618         return iter->second;
619 }
620
621
622 optional<wxTreeListItem>
623 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
624 {
625         auto iter = _cinema_to_item.find (cinema);
626         if (iter == _cinema_to_item.end()) {
627                 return {};
628         }
629
630         return iter->second;
631 }
632
633
634 optional<wxTreeListItem>
635 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
636 {
637         auto iter = _screen_to_item.find (screen);
638         if (iter == _screen_to_item.end()) {
639                 return {};
640         }
641
642         return iter->second;
643 }
644
645
646 int
647 ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
648 {
649         if (_collator) {
650                 UErrorCode error = U_ZERO_ERROR;
651                 boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
652                 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
653                 boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
654                 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
655                 return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
656         } else {
657                 return strcoll(utf8_a.c_str(), utf8_b.c_str());
658         }
659 }