Speed things up by sorting before 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/screen.h"
30 #include "lib/timer.h"
31 #include <unicode/putil.h>
32 #include <unicode/ucol.h>
33 #include <unicode/uiter.h>
34 #include <unicode/utypes.h>
35 #include <unicode/ustring.h>
36
37
38 using std::cout;
39 using std::make_pair;
40 using std::make_shared;
41 using std::map;
42 using std::pair;
43 using std::shared_ptr;
44 using std::string;
45 using std::vector;
46 using boost::optional;
47 using namespace dcpomatic;
48
49
50 ScreensPanel::ScreensPanel (wxWindow* parent)
51         : wxPanel (parent, wxID_ANY)
52 {
53         auto sizer = new wxBoxSizer (wxVERTICAL);
54
55         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
56 #ifndef __WXGTK3__
57         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
58         _search->ShowCancelButton (true);
59 #endif
60         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
61
62         auto targets = new wxBoxSizer (wxHORIZONTAL);
63         _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
64         _targets->AppendColumn (wxT("foo"));
65
66         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
67
68         add_cinemas ();
69
70         auto target_buttons = new wxBoxSizer (wxVERTICAL);
71
72         _add_cinema = new Button (this, _("Add Cinema..."));
73         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
74         _edit_cinema = new Button (this, _("Edit Cinema..."));
75         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76         _remove_cinema = new Button (this, _("Remove Cinema"));
77         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78         _add_screen = new Button (this, _("Add Screen..."));
79         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
80         _edit_screen = new Button (this, _("Edit Screen..."));
81         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
82         _remove_screen = new Button (this, _("Remove Screen"));
83         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84
85         targets->Add (target_buttons, 0, 0);
86
87         sizer->Add (targets, 1, wxEXPAND);
88
89         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
90         _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
91         _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
92
93         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
94         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
95         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
96
97         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
98         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
99         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
100
101         SetSizer (sizer);
102
103         UErrorCode status = U_ZERO_ERROR;
104         _collator = ucol_open(nullptr, &status);
105         if (_collator) {
106                 ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
107                 ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
108                 ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
109         }
110 }
111
112
113 ScreensPanel::~ScreensPanel ()
114 {
115         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
116         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
117
118         if (_collator) {
119                 ucol_close (_collator);
120         }
121 }
122
123
124 void
125 ScreensPanel::setup_sensitivity ()
126 {
127         bool const sc = _selected_cinemas.size() == 1;
128         bool const ss = _selected_screens.size() == 1;
129
130         _edit_cinema->Enable (sc || ss);
131         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
132
133         _add_screen->Enable (sc || ss);
134         _edit_screen->Enable (ss);
135         _remove_screen->Enable (_selected_screens.size() >= 1);
136 }
137
138
139 optional<wxTreeListItem>
140 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
141 {
142         auto search = wx_to_std (_search->GetValue ());
143         transform (search.begin(), search.end(), search.begin(), ::tolower);
144
145         if (!search.empty ()) {
146                 auto name = cinema->name;
147                 transform (name.begin(), name.end(), name.begin(), ::tolower);
148                 if (name.find (search) == string::npos) {
149                         return {};
150                 }
151         }
152
153         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
154
155         _cinemas.push_back(make_pair(id, cinema));
156         _item_to_cinema[id] = cinema;
157         _cinema_to_item[cinema] = id;
158
159         for (auto screen: cinema->screens()) {
160                 add_screen (cinema, screen);
161         }
162
163         return id;
164 }
165
166
167 optional<wxTreeListItem>
168 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
169 {
170         auto item = cinema_to_item(cinema);
171         if (!item) {
172                 return {};
173         }
174
175         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
176
177         _screens.push_back(make_pair(id, screen));
178         _item_to_screen[id] = screen;
179         _screen_to_item[screen] = id;
180
181         return item;
182 }
183
184
185 void
186 ScreensPanel::add_cinema_clicked ()
187 {
188         auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
189         if (d->ShowModal () == wxID_OK) {
190                 auto cinema = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
191
192                 auto cinemas = Config::instance()->cinemas();
193                 cinemas.sort(
194                         [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
195                         );
196
197                 optional<wxTreeListItem> item;
198                 for (auto existing_cinema: cinemas) {
199                         if (!item && compare(d->name(), existing_cinema->name) < 0) {
200                                 if (auto existing_item = cinema_to_item(existing_cinema)) {
201                                         item = add_cinema (cinema, *existing_item);
202                                 }
203                         }
204                 }
205
206                 if (!item) {
207                         item = add_cinema (cinema, wxTLI_LAST);
208                 }
209
210                 if (item) {
211                         _targets->UnselectAll ();
212                         _targets->Select (*item);
213                 }
214
215                 Config::instance()->add_cinema (cinema);
216         }
217
218         d->Destroy ();
219 }
220
221
222 optional<pair<wxTreeListItem, shared_ptr<Cinema>>>
223 ScreensPanel::cinema_for_operation () const
224 {
225         if (_selected_cinemas.size() == 1) {
226                 return make_pair(_selected_cinemas.begin()->first, _selected_cinemas.begin()->second);
227         } else if (_selected_screens.size() == 1) {
228                 return make_pair(_targets->GetItemParent(_selected_screens.begin()->first), _selected_screens.begin()->second->cinema);
229         }
230
231         return {};
232 }
233
234
235 void
236 ScreensPanel::edit_cinema_clicked ()
237 {
238         auto cinema = cinema_for_operation ();
239         if (!cinema) {
240                 return;
241         }
242
243         auto d = new CinemaDialog (
244                 GetParent(), _("Edit cinema"), cinema->second->name, cinema->second->emails, cinema->second->notes, cinema->second->utc_offset_hour(), cinema->second->utc_offset_minute()
245                 );
246
247         if (d->ShowModal() == wxID_OK) {
248                 cinema->second->name = d->name ();
249                 cinema->second->emails = d->emails ();
250                 cinema->second->notes = d->notes ();
251                 cinema->second->set_utc_offset_hour (d->utc_offset_hour ());
252                 cinema->second->set_utc_offset_minute (d->utc_offset_minute ());
253                 _targets->SetItemText (cinema->first, std_to_wx(d->name()));
254                 Config::instance()->changed (Config::CINEMAS);
255         }
256
257         d->Destroy ();
258 }
259
260
261 void
262 ScreensPanel::remove_cinema_clicked ()
263 {
264         if (_selected_cinemas.size() == 1) {
265                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
266                         return;
267                 }
268         } else {
269                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
270                         return;
271                 }
272         }
273
274         for (auto const& i: _selected_cinemas) {
275                 Config::instance()->remove_cinema (i.second);
276                 _targets->DeleteItem (i.first);
277         }
278
279         selection_changed ();
280 }
281
282
283 void
284 ScreensPanel::add_screen_clicked ()
285 {
286         auto cinema = cinema_for_operation ();
287         if (!cinema) {
288                 return;
289         }
290
291         auto d = new ScreenDialog (GetParent(), _("Add Screen"));
292         if (d->ShowModal () != wxID_OK) {
293                 d->Destroy ();
294                 return;
295         }
296
297         for (auto screen: cinema->second->screens()) {
298                 if (screen->name == d->name()) {
299                         error_dialog (
300                                 GetParent(),
301                                 wxString::Format (
302                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
303                                         std_to_wx(d->name()).data()
304                                         )
305                                 );
306                         return;
307                 }
308         }
309
310         auto screen = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
311         cinema->second->add_screen (screen);
312         auto id = add_screen (cinema->second, screen);
313         if (id) {
314                 _targets->Expand (id.get ());
315         }
316
317         Config::instance()->changed (Config::CINEMAS);
318
319         d->Destroy ();
320 }
321
322
323 void
324 ScreensPanel::edit_screen_clicked ()
325 {
326         if (_selected_screens.size() != 1) {
327                 return;
328         }
329
330         auto edit_screen = *_selected_screens.begin();
331
332         auto d = new ScreenDialog (
333                 GetParent(), _("Edit screen"),
334                 edit_screen.second->name,
335                 edit_screen.second->notes,
336                 edit_screen.second->recipient,
337                 edit_screen.second->recipient_file,
338                 edit_screen.second->trusted_devices
339                 );
340
341         if (d->ShowModal() != wxID_OK) {
342                 d->Destroy ();
343                 return;
344         }
345
346         auto cinema = edit_screen.second->cinema;
347         for (auto screen: cinema->screens()) {
348                 if (screen != edit_screen.second && screen->name == d->name()) {
349                         error_dialog (
350                                 GetParent(),
351                                 wxString::Format (
352                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
353                                         std_to_wx(d->name()).data()
354                                         )
355                                 );
356                         return;
357                 }
358         }
359
360         edit_screen.second->name = d->name ();
361         edit_screen.second->notes = d->notes ();
362         edit_screen.second->recipient = d->recipient ();
363         edit_screen.second->recipient_file = d->recipient_file ();
364         edit_screen.second->trusted_devices = d->trusted_devices ();
365         _targets->SetItemText (edit_screen.first, std_to_wx(d->name()));
366         Config::instance()->changed (Config::CINEMAS);
367
368         d->Destroy ();
369 }
370
371
372 void
373 ScreensPanel::remove_screen_clicked ()
374 {
375         if (_selected_screens.size() == 1) {
376                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
377                         return;
378                 }
379         } else {
380                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
381                         return;
382                 }
383         }
384
385         for (auto const& i: _selected_screens) {
386                 auto j = _cinemas.begin ();
387                 while (j != _cinemas.end ()) {
388                         auto sc = j->second->screens ();
389                         if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
390                                 break;
391                         }
392
393                         ++j;
394                 }
395
396                 if (j == _cinemas.end()) {
397                         continue;
398                 }
399
400                 j->second->remove_screen (i.second);
401                 _targets->DeleteItem (i.first);
402         }
403
404         Config::instance()->changed (Config::CINEMAS);
405 }
406
407
408 vector<shared_ptr<Screen>>
409 ScreensPanel::screens () const
410 {
411         vector<shared_ptr<Screen>> output;
412
413         for (auto item = _targets->GetFirstItem(); item.IsOk(); item = _targets->GetNextItem(item)) {
414                 if (_targets->GetCheckedState(item) == wxCHK_CHECKED) {
415                         if (auto screen = item_to_screen(item)) {
416                                 output.push_back (screen);
417                         }
418                 }
419         }
420
421         return output;
422 }
423
424
425 void
426 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
427 {
428         selection_changed ();
429 }
430
431
432 void
433 ScreensPanel::selection_changed ()
434 {
435         if (_ignore_selection_change) {
436                 return;
437         }
438
439         wxTreeListItems selection;
440         _targets->GetSelections (selection);
441
442         _selected_cinemas.clear ();
443         _selected_screens.clear ();
444
445         for (size_t i = 0; i < selection.size(); ++i) {
446                 if (auto cinema = item_to_cinema(selection[i])) {
447                         _selected_cinemas.push_back(make_pair(selection[i], cinema));
448                 }
449                 if (auto screen = item_to_screen(selection[i])) {
450                         _selected_screens.push_back(make_pair(selection[i], screen));
451                 }
452         }
453
454         setup_sensitivity ();
455 }
456
457
458 void
459 ScreensPanel::add_cinemas ()
460 {
461         auto cinemas = Config::instance()->cinemas();
462         cinemas.sort(
463                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
464                 );
465
466         for (auto cinema: cinemas) {
467                 add_cinema (cinema, wxTLI_LAST);
468         }
469 }
470
471
472 void
473 ScreensPanel::search_changed ()
474 {
475         _targets->DeleteAllItems ();
476         _cinemas.clear ();
477         _screens.clear ();
478
479         _item_to_cinema.clear ();
480         _cinema_to_item.clear ();
481         _item_to_screen.clear ();
482         _screen_to_item.clear ();
483
484         add_cinemas ();
485
486         _ignore_selection_change = true;
487
488         for (auto const& selection: _selected_cinemas) {
489                 /* The wxTreeListItems will now be different, so we must search by cinema */
490                 if (auto item = cinema_to_item(selection.second)) {
491                         _targets->Select (*item);
492                 }
493         }
494
495         for (auto const& selection: _selected_screens) {
496                 /* Likewise by screen */
497                 if (auto item = screen_to_item(selection.second)) {
498                         _targets->Select (*item);
499                 }
500         }
501
502         _ignore_selection_change = false;
503
504         _ignore_check_change = true;
505
506         for (auto const& checked: _checked_screens) {
507                 if (auto item = screen_to_item(checked.second)) {
508                         _targets->CheckItem(*item, wxCHK_CHECKED);
509                         setup_cinema_checked_state(*item);
510                 }
511         }
512
513         _ignore_check_change = false;
514 }
515
516
517 void
518 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
519 {
520         auto current = std::find_if(
521                 _checked_screens.begin(), _checked_screens.end(),
522                 [item](pair<wxTreeListItem, shared_ptr<Screen>> const& screen) { return screen.first == item; }
523                 );
524
525         if (current == _checked_screens.end() && checked) {
526                 _checked_screens.push_back({item, item_to_screen(item)});
527         } else if (current != _checked_screens.end() && !checked) {
528                 _checked_screens.erase(current);
529         }
530 }
531
532
533 void
534 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
535 {
536         auto cinema = _targets->GetItemParent(screen);
537         DCPOMATIC_ASSERT (cinema.IsOk());
538         int checked = 0;
539         int unchecked = 0;
540         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
541                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
542                     ++checked;
543                 } else {
544                     ++unchecked;
545                 }
546         }
547         if (checked == 0) {
548                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
549         } else if (unchecked == 0) {
550                 _targets->CheckItem(cinema, wxCHK_CHECKED);
551         } else {
552                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
553         }
554 }
555
556
557 void
558 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
559 {
560         if (_ignore_check_change) {
561                 return;
562         }
563
564         if (item_to_cinema(ev.GetItem())) {
565                 /* Cinema: check/uncheck all children */
566                 auto const checked = _targets->GetCheckedState(ev.GetItem());
567                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
568                         _targets->CheckItem(child, checked);
569                         set_screen_checked(child, checked);
570                 }
571         } else {
572                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
573                 setup_cinema_checked_state(ev.GetItem());
574         }
575
576         ScreensChanged ();
577 }
578
579
580 shared_ptr<Cinema>
581 ScreensPanel::item_to_cinema (wxTreeListItem item) const
582 {
583         auto iter = _item_to_cinema.find (item);
584         if (iter == _item_to_cinema.end()) {
585                 return {};
586         }
587
588         return iter->second;
589 }
590
591
592 shared_ptr<Screen>
593 ScreensPanel::item_to_screen (wxTreeListItem item) const
594 {
595         auto iter = _item_to_screen.find (item);
596         if (iter == _item_to_screen.end()) {
597                 return {};
598         }
599
600         return iter->second;
601 }
602
603
604 optional<wxTreeListItem>
605 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
606 {
607         auto iter = _cinema_to_item.find (cinema);
608         if (iter == _cinema_to_item.end()) {
609                 return {};
610         }
611
612         return iter->second;
613 }
614
615
616 optional<wxTreeListItem>
617 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
618 {
619         auto iter = _screen_to_item.find (screen);
620         if (iter == _screen_to_item.end()) {
621                 return {};
622         }
623
624         return iter->second;
625 }
626
627
628 int
629 ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
630 {
631         if (_collator) {
632                 UErrorCode error = U_ZERO_ERROR;
633                 boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
634                 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
635                 boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
636                 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
637                 return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
638         } else {
639                 return strcoll(utf8_a.c_str(), utf8_b.c_str());
640         }
641 }