2 Copyright (C) 2015-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "cinema_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.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>
41 using std::make_shared;
44 using std::shared_ptr;
47 using boost::optional;
48 using namespace dcpomatic;
51 ScreensPanel::ScreensPanel (wxWindow* parent)
52 : wxPanel (parent, wxID_ANY)
54 auto sizer = new wxBoxSizer (wxVERTICAL);
56 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
58 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
59 _search->ShowCancelButton (true);
61 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
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"));
67 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
71 auto side_buttons = new wxBoxSizer (wxVERTICAL);
73 auto target_buttons = new wxBoxSizer (wxVERTICAL);
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);
88 side_buttons->Add (target_buttons, 0, 0);
90 auto check_buttons = new wxBoxSizer (wxVERTICAL);
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);
97 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
99 targets->Add (side_buttons, 0, 0);
101 sizer->Add (targets, 1, wxEXPAND);
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);
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));
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));
115 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
116 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
120 UErrorCode status = U_ZERO_ERROR;
121 _collator = ucol_open(nullptr, &status);
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);
130 ScreensPanel::~ScreensPanel ()
132 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
133 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
136 ucol_close (_collator);
142 ScreensPanel::check_all ()
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);
155 ScreensPanel::uncheck_all ()
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);
168 ScreensPanel::setup_sensitivity ()
170 bool const sc = _selected_cinemas.size() == 1;
171 bool const ss = _selected_screens.size() == 1;
173 _edit_cinema->Enable (sc || ss);
174 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
176 _add_screen->Enable (sc || ss);
177 _edit_screen->Enable (ss);
178 _remove_screen->Enable (_selected_screens.size() >= 1);
182 optional<wxTreeListItem>
183 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
185 auto search = wx_to_std (_search->GetValue ());
186 transform (search.begin(), search.end(), search.begin(), ::tolower);
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) {
196 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
198 _item_to_cinema[id] = cinema;
199 _cinema_to_item[cinema] = id;
201 for (auto screen: cinema->screens()) {
202 add_screen (cinema, screen);
209 optional<wxTreeListItem>
210 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
212 auto item = cinema_to_item(cinema);
217 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
219 _item_to_screen[id] = screen;
220 _screen_to_item[screen] = id;
227 ScreensPanel::add_cinema_clicked ()
229 auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
230 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
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());
235 auto cinemas = Config::instance()->cinemas();
237 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
241 Config::instance()->add_cinema(cinema);
242 } catch (FileError& e) {
243 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 optional<wxTreeListItem> item;
248 for (auto existing_cinema: cinemas) {
249 if (!item && compare(dialog->name(), existing_cinema->name) < 0) {
250 if (auto existing_item = cinema_to_item(existing_cinema)) {
251 item = add_cinema (cinema, *existing_item);
257 item = add_cinema (cinema, wxTLI_LAST);
261 _targets->UnselectAll ();
262 _targets->Select (*item);
266 selection_changed ();
271 ScreensPanel::cinema_for_operation () const
273 if (_selected_cinemas.size() == 1) {
274 return _selected_cinemas[0];
275 } else if (_selected_screens.size() == 1) {
276 return _selected_screens[0]->cinema;
284 ScreensPanel::edit_cinema_clicked ()
286 auto cinema = cinema_for_operation ();
291 auto dialog = new CinemaDialog(
292 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
294 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
296 if (dialog->ShowModal() == wxID_OK) {
297 cinema->name = dialog->name();
298 cinema->emails = dialog->emails();
299 cinema->notes = dialog->notes();
300 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
301 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
302 notify_cinemas_changed();
303 auto item = cinema_to_item(cinema);
304 DCPOMATIC_ASSERT(item);
305 _targets->SetItemText (*item, std_to_wx(dialog->name()));
311 ScreensPanel::remove_cinema_clicked ()
313 if (_selected_cinemas.size() == 1) {
314 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
318 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
323 for (auto const& cinema: _selected_cinemas) {
324 Config::instance()->remove_cinema(cinema);
325 auto item = cinema_to_item(cinema);
326 DCPOMATIC_ASSERT(item);
327 _targets->DeleteItem(*item);
330 selection_changed ();
335 ScreensPanel::add_screen_clicked ()
337 auto cinema = cinema_for_operation ();
342 auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
343 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
345 if (dialog->ShowModal () != wxID_OK) {
349 for (auto screen: cinema->screens()) {
350 if (screen->name == dialog->name()) {
354 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
355 std_to_wx(dialog->name()).data()
362 auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
363 cinema->add_screen (screen);
364 notify_cinemas_changed();
366 auto id = add_screen (cinema, screen);
368 _targets->Expand (id.get ());
374 ScreensPanel::edit_screen_clicked ()
376 if (_selected_screens.size() != 1) {
380 auto edit_screen = _selected_screens[0];
382 auto dialog = new ScreenDialog(
383 GetParent(), _("Edit screen"),
386 edit_screen->recipient,
387 edit_screen->recipient_file,
388 edit_screen->trusted_devices
390 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
392 if (dialog->ShowModal() != wxID_OK) {
396 auto cinema = edit_screen->cinema;
397 for (auto screen: cinema->screens()) {
398 if (screen != edit_screen && screen->name == dialog->name()) {
402 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
403 std_to_wx(dialog->name()).data()
410 edit_screen->name = dialog->name();
411 edit_screen->notes = dialog->notes();
412 edit_screen->recipient = dialog->recipient();
413 edit_screen->recipient_file = dialog->recipient_file();
414 edit_screen->trusted_devices = dialog->trusted_devices();
415 notify_cinemas_changed();
417 auto item = screen_to_item(edit_screen);
418 DCPOMATIC_ASSERT (item);
419 _targets->SetItemText (*item, std_to_wx(dialog->name()));
424 ScreensPanel::remove_screen_clicked ()
426 if (_selected_screens.size() == 1) {
427 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
431 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
436 for (auto const& screen: _selected_screens) {
437 screen->cinema->remove_screen(screen);
438 auto item = screen_to_item(screen);
439 DCPOMATIC_ASSERT(item);
440 _targets->DeleteItem(*item);
443 notify_cinemas_changed();
447 vector<shared_ptr<Screen>>
448 ScreensPanel::screens () const
450 vector<shared_ptr<Screen>> output;
451 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
457 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
459 selection_changed ();
464 ScreensPanel::selection_changed ()
466 if (_ignore_selection_change) {
470 wxTreeListItems selection;
471 _targets->GetSelections (selection);
473 _selected_cinemas.clear ();
474 _selected_screens.clear ();
476 for (size_t i = 0; i < selection.size(); ++i) {
477 if (auto cinema = item_to_cinema(selection[i])) {
478 _selected_cinemas.push_back(cinema);
480 if (auto screen = item_to_screen(selection[i])) {
481 _selected_screens.push_back(screen);
485 setup_sensitivity ();
490 ScreensPanel::add_cinemas ()
492 auto cinemas = Config::instance()->cinemas();
494 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
497 for (auto cinema: cinemas) {
498 add_cinema (cinema, wxTLI_LAST);
504 ScreensPanel::search_changed ()
506 _targets->DeleteAllItems ();
508 _item_to_cinema.clear ();
509 _cinema_to_item.clear ();
510 _item_to_screen.clear ();
511 _screen_to_item.clear ();
515 _ignore_selection_change = true;
517 for (auto const& selection: _selected_cinemas) {
518 if (auto item = cinema_to_item(selection)) {
519 _targets->Select (*item);
523 for (auto const& selection: _selected_screens) {
524 if (auto item = screen_to_item(selection)) {
525 _targets->Select (*item);
529 _ignore_selection_change = false;
531 _ignore_check_change = true;
533 for (auto const& checked: _checked_screens) {
534 if (auto item = screen_to_item(checked)) {
535 _targets->CheckItem(*item, wxCHK_CHECKED);
536 setup_cinema_checked_state(*item);
540 _ignore_check_change = false;
545 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
547 auto screen = item_to_screen(item);
548 DCPOMATIC_ASSERT(screen);
550 _checked_screens.insert(screen);
552 _checked_screens.erase(screen);
558 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
560 auto cinema = _targets->GetItemParent(screen);
561 DCPOMATIC_ASSERT (cinema.IsOk());
564 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
565 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
572 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
573 } else if (unchecked == 0) {
574 _targets->CheckItem(cinema, wxCHK_CHECKED);
576 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
582 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
584 if (_ignore_check_change) {
588 if (item_to_cinema(ev.GetItem())) {
589 /* Cinema: check/uncheck all children */
590 auto const checked = _targets->GetCheckedState(ev.GetItem());
591 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
592 _targets->CheckItem(child, checked);
593 set_screen_checked(child, checked);
596 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
597 setup_cinema_checked_state(ev.GetItem());
605 ScreensPanel::item_to_cinema (wxTreeListItem item) const
607 auto iter = _item_to_cinema.find (item);
608 if (iter == _item_to_cinema.end()) {
617 ScreensPanel::item_to_screen (wxTreeListItem item) const
619 auto iter = _item_to_screen.find (item);
620 if (iter == _item_to_screen.end()) {
628 optional<wxTreeListItem>
629 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
631 auto iter = _cinema_to_item.find (cinema);
632 if (iter == _cinema_to_item.end()) {
640 optional<wxTreeListItem>
641 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
643 auto iter = _screen_to_item.find (screen);
644 if (iter == _screen_to_item.end()) {
653 ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
656 UErrorCode error = U_ZERO_ERROR;
657 boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
658 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
659 boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
660 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
661 return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
663 return strcoll(utf8_a.c_str(), utf8_b.c_str());
669 ScreensPanel::notify_cinemas_changed()
672 Config::instance()->changed(Config::CINEMAS);
673 } catch (FileError& e) {
674 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()));