Now we don't need _cinemas and _screens any more.
[dcpomatic.git] / src / wx / screens_panel.cc
index 34bdadd8ea35b91014c5efcf739ec6d4a5ebc966..fbff896ca401581ac47104e75faf5a7438cd3714 100644 (file)
@@ -27,6 +27,7 @@
 #include "lib/cinema.h"
 #include "lib/config.h"
 #include "lib/screen.h"
+#include "lib/timer.h"
 #include <unicode/putil.h>
 #include <unicode/ucol.h>
 #include <unicode/uiter.h>
@@ -61,8 +62,6 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        auto targets = new wxBoxSizer (wxHORIZONTAL);
        _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
        _targets->AppendColumn (wxT("foo"));
-       _targets->SetSortColumn (0);
-       _targets->SetItemComparator (&_comparator);
 
        targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
 
@@ -100,6 +99,14 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
 
        SetSizer (sizer);
+
+       UErrorCode status = U_ZERO_ERROR;
+       _collator = ucol_open(nullptr, &status);
+       if (_collator) {
+               ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+               ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
+               ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
+       }
 }
 
 
@@ -107,6 +114,10 @@ ScreensPanel::~ScreensPanel ()
 {
        _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
        _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
+
+       if (_collator) {
+               ucol_close (_collator);
+       }
 }
 
 
@@ -126,7 +137,7 @@ ScreensPanel::setup_sensitivity ()
 
 
 optional<wxTreeListItem>
-ScreensPanel::add_cinema (shared_ptr<Cinema> cinema)
+ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
 {
        auto search = wx_to_std (_search->GetValue ());
        transform (search.begin(), search.end(), search.begin(), ::tolower);
@@ -139,9 +150,8 @@ ScreensPanel::add_cinema (shared_ptr<Cinema> cinema)
                }
        }
 
-       auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(cinema->name));
+       auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
 
-       _cinemas.push_back(make_pair(id, cinema));
        _item_to_cinema[id] = cinema;
        _cinema_to_item[cinema] = id;
 
@@ -163,7 +173,6 @@ ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
 
        auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
 
-       _screens.push_back(make_pair(id, screen));
        _item_to_screen[id] = screen;
        _screen_to_item[screen] = id;
 
@@ -177,25 +186,44 @@ ScreensPanel::add_cinema_clicked ()
        auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
        if (d->ShowModal () == wxID_OK) {
                auto cinema = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
-               Config::instance()->add_cinema (cinema);
-               auto id = add_cinema (cinema);
-               if (id) {
+
+               auto cinemas = Config::instance()->cinemas();
+               cinemas.sort(
+                       [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
+                       );
+
+               optional<wxTreeListItem> item;
+               for (auto existing_cinema: cinemas) {
+                       if (!item && compare(d->name(), existing_cinema->name) < 0) {
+                               if (auto existing_item = cinema_to_item(existing_cinema)) {
+                                       item = add_cinema (cinema, *existing_item);
+                               }
+                       }
+               }
+
+               if (!item) {
+                       item = add_cinema (cinema, wxTLI_LAST);
+               }
+
+               if (item) {
                        _targets->UnselectAll ();
-                       _targets->Select (*id);
+                       _targets->Select (*item);
                }
+
+               Config::instance()->add_cinema (cinema);
        }
 
        d->Destroy ();
 }
 
 
-optional<pair<wxTreeListItem, shared_ptr<Cinema>>>
+shared_ptr<Cinema>
 ScreensPanel::cinema_for_operation () const
 {
        if (_selected_cinemas.size() == 1) {
-               return make_pair(_selected_cinemas.begin()->first, _selected_cinemas.begin()->second);
+               return _selected_cinemas[0];
        } else if (_selected_screens.size() == 1) {
-               return make_pair(_targets->GetItemParent(_selected_screens.begin()->first), _selected_screens.begin()->second->cinema);
+               return _selected_screens[0]->cinema;
        }
 
        return {};
@@ -211,16 +239,18 @@ ScreensPanel::edit_cinema_clicked ()
        }
 
        auto d = new CinemaDialog (
-               GetParent(), _("Edit cinema"), cinema->second->name, cinema->second->emails, cinema->second->notes, cinema->second->utc_offset_hour(), cinema->second->utc_offset_minute()
+               GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
                );
 
        if (d->ShowModal() == wxID_OK) {
-               cinema->second->name = d->name ();
-               cinema->second->emails = d->emails ();
-               cinema->second->notes = d->notes ();
-               cinema->second->set_utc_offset_hour (d->utc_offset_hour ());
-               cinema->second->set_utc_offset_minute (d->utc_offset_minute ());
-               _targets->SetItemText (cinema->first, std_to_wx(d->name()));
+               cinema->name = d->name();
+               cinema->emails = d->emails();
+               cinema->notes = d->notes();
+               cinema->set_utc_offset_hour(d->utc_offset_hour());
+               cinema->set_utc_offset_minute(d->utc_offset_minute());
+               auto item = cinema_to_item(cinema);
+               DCPOMATIC_ASSERT(item);
+               _targets->SetItemText (*item, std_to_wx(d->name()));
                Config::instance()->changed (Config::CINEMAS);
        }
 
@@ -232,7 +262,7 @@ void
 ScreensPanel::remove_cinema_clicked ()
 {
        if (_selected_cinemas.size() == 1) {
-               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
                        return;
                }
        } else {
@@ -241,9 +271,11 @@ ScreensPanel::remove_cinema_clicked ()
                }
        }
 
-       for (auto const& i: _selected_cinemas) {
-               Config::instance()->remove_cinema (i.second);
-               _targets->DeleteItem (i.first);
+       for (auto const& cinema: _selected_cinemas) {
+               Config::instance()->remove_cinema(cinema);
+               auto item = cinema_to_item(cinema);
+               DCPOMATIC_ASSERT(item);
+               _targets->DeleteItem(*item);
        }
 
        selection_changed ();
@@ -264,7 +296,7 @@ ScreensPanel::add_screen_clicked ()
                return;
        }
 
-       for (auto screen: cinema->second->screens()) {
+       for (auto screen: cinema->screens()) {
                if (screen->name == d->name()) {
                        error_dialog (
                                GetParent(),
@@ -278,8 +310,8 @@ ScreensPanel::add_screen_clicked ()
        }
 
        auto screen = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
-       cinema->second->add_screen (screen);
-       auto id = add_screen (cinema->second, screen);
+       cinema->add_screen (screen);
+       auto id = add_screen (cinema, screen);
        if (id) {
                _targets->Expand (id.get ());
        }
@@ -297,15 +329,15 @@ ScreensPanel::edit_screen_clicked ()
                return;
        }
 
-       auto edit_screen = *_selected_screens.begin();
+       auto edit_screen = _selected_screens[0];
 
        auto d = new ScreenDialog (
                GetParent(), _("Edit screen"),
-               edit_screen.second->name,
-               edit_screen.second->notes,
-               edit_screen.second->recipient,
-               edit_screen.second->recipient_file,
-               edit_screen.second->trusted_devices
+               edit_screen->name,
+               edit_screen->notes,
+               edit_screen->recipient,
+               edit_screen->recipient_file,
+               edit_screen->trusted_devices
                );
 
        if (d->ShowModal() != wxID_OK) {
@@ -313,9 +345,9 @@ ScreensPanel::edit_screen_clicked ()
                return;
        }
 
-       auto cinema = edit_screen.second->cinema;
+       auto cinema = edit_screen->cinema;
        for (auto screen: cinema->screens()) {
-               if (screen != edit_screen.second && screen->name == d->name()) {
+               if (screen != edit_screen && screen->name == d->name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
@@ -327,12 +359,14 @@ ScreensPanel::edit_screen_clicked ()
                }
        }
 
-       edit_screen.second->name = d->name ();
-       edit_screen.second->notes = d->notes ();
-       edit_screen.second->recipient = d->recipient ();
-       edit_screen.second->recipient_file = d->recipient_file ();
-       edit_screen.second->trusted_devices = d->trusted_devices ();
-       _targets->SetItemText (edit_screen.first, std_to_wx(d->name()));
+       edit_screen->name = d->name();
+       edit_screen->notes = d->notes();
+       edit_screen->recipient = d->recipient();
+       edit_screen->recipient_file = d->recipient_file();
+       edit_screen->trusted_devices = d->trusted_devices();
+       auto item = screen_to_item(edit_screen);
+       DCPOMATIC_ASSERT (item);
+       _targets->SetItemText (*item, std_to_wx(d->name()));
        Config::instance()->changed (Config::CINEMAS);
 
        d->Destroy ();
@@ -343,7 +377,7 @@ void
 ScreensPanel::remove_screen_clicked ()
 {
        if (_selected_screens.size() == 1) {
-               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
                        return;
                }
        } else {
@@ -352,23 +386,11 @@ ScreensPanel::remove_screen_clicked ()
                }
        }
 
-       for (auto const& i: _selected_screens) {
-               auto j = _cinemas.begin ();
-               while (j != _cinemas.end ()) {
-                       auto sc = j->second->screens ();
-                       if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
-                               break;
-                       }
-
-                       ++j;
-               }
-
-               if (j == _cinemas.end()) {
-                       continue;
-               }
-
-               j->second->remove_screen (i.second);
-               _targets->DeleteItem (i.first);
+       for (auto const& screen: _selected_screens) {
+               screen->cinema->remove_screen(screen);
+               auto item = screen_to_item(screen);
+               DCPOMATIC_ASSERT(item);
+               _targets->DeleteItem(*item);
        }
 
        Config::instance()->changed (Config::CINEMAS);
@@ -414,10 +436,10 @@ ScreensPanel::selection_changed ()
 
        for (size_t i = 0; i < selection.size(); ++i) {
                if (auto cinema = item_to_cinema(selection[i])) {
-                       _selected_cinemas.push_back(make_pair(selection[i], cinema));
+                       _selected_cinemas.push_back(cinema);
                }
                if (auto screen = item_to_screen(selection[i])) {
-                       _selected_screens.push_back(make_pair(selection[i], screen));
+                       _selected_screens.push_back(screen);
                }
        }
 
@@ -428,8 +450,13 @@ ScreensPanel::selection_changed ()
 void
 ScreensPanel::add_cinemas ()
 {
-       for (auto cinema: Config::instance()->cinemas()) {
-               add_cinema (cinema);
+       auto cinemas = Config::instance()->cinemas();
+       cinemas.sort(
+               [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
+               );
+
+       for (auto cinema: cinemas) {
+               add_cinema (cinema, wxTLI_LAST);
        }
 }
 
@@ -438,8 +465,6 @@ void
 ScreensPanel::search_changed ()
 {
        _targets->DeleteAllItems ();
-       _cinemas.clear ();
-       _screens.clear ();
 
        _item_to_cinema.clear ();
        _cinema_to_item.clear ();
@@ -451,15 +476,13 @@ ScreensPanel::search_changed ()
        _ignore_selection_change = true;
 
        for (auto const& selection: _selected_cinemas) {
-               /* The wxTreeListItems will now be different, so we must search by cinema */
-               if (auto item = cinema_to_item(selection.second)) {
+               if (auto item = cinema_to_item(selection)) {
                        _targets->Select (*item);
                }
        }
 
        for (auto const& selection: _selected_screens) {
-               /* Likewise by screen */
-               if (auto item = screen_to_item(selection.second)) {
+               if (auto item = screen_to_item(selection)) {
                        _targets->Select (*item);
                }
        }
@@ -469,7 +492,7 @@ ScreensPanel::search_changed ()
        _ignore_check_change = true;
 
        for (auto const& checked: _checked_screens) {
-               if (auto item = screen_to_item(checked.second)) {
+               if (auto item = screen_to_item(checked)) {
                        _targets->CheckItem(*item, wxCHK_CHECKED);
                        setup_cinema_checked_state(*item);
                }
@@ -482,15 +505,12 @@ ScreensPanel::search_changed ()
 void
 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
 {
-       auto current = std::find_if(
-               _checked_screens.begin(), _checked_screens.end(),
-               [item](pair<wxTreeListItem, shared_ptr<Screen>> const& screen) { return screen.first == item; }
-               );
-
-       if (current == _checked_screens.end() && checked) {
-               _checked_screens.push_back({item, item_to_screen(item)});
-       } else if (current != _checked_screens.end() && !checked) {
-               _checked_screens.erase(current);
+       auto screen = item_to_screen(item);
+       DCPOMATIC_ASSERT(screen);
+       if (checked) {
+               _checked_screens.insert(screen);
+       } else {
+               _checked_screens.erase(screen);
        }
 }
 
@@ -590,29 +610,9 @@ ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
 }
 
 
-ScreensPanel::Comparator::Comparator ()
-{
-       UErrorCode status = U_ZERO_ERROR;
-       _collator = ucol_open(nullptr, &status);
-       if (_collator) {
-               ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
-               ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
-               ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
-       }
-}
-
-ScreensPanel::Comparator::~Comparator ()
-{
-       if (_collator) {
-               ucol_close (_collator);
-       }
-}
-
 int
-ScreensPanel::Comparator::Compare (wxTreeListCtrl* tree_list, unsigned, wxTreeListItem a, wxTreeListItem b)
+ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
 {
-       auto utf8_a = wx_to_std(tree_list->GetItemText(a));
-       auto utf8_b = wx_to_std(tree_list->GetItemText(b));
        if (_collator) {
                UErrorCode error = U_ZERO_ERROR;
                boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);