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