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