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