Use collator to search for screens (#2426).
[dcpomatic.git] / src / wx / screens_panel.cc
index 562ea8f3895d0d67423ee135ae8b76bf4ef35900..6c5b28b8d0fdd048449ce2c0e3a0bdfc8994d3a3 100644 (file)
 #include "lib/scope_guard.h"
 #include "lib/screen.h"
 #include "lib/timer.h"
-#include <unicode/putil.h>
-#include <unicode/ucol.h>
-#include <unicode/uiter.h>
-#include <unicode/utypes.h>
-#include <unicode/ustring.h>
 
 
 using std::cout;
+using std::list;
 using std::make_pair;
 using std::make_shared;
 using std::map;
@@ -106,6 +102,7 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
        _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
        _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
+       _targets->Bind       (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
 
        _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
        _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
@@ -120,14 +117,6 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
 
        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);
-       }
-
        _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
 }
 
@@ -136,10 +125,6 @@ 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);
-       }
 }
 
 
@@ -184,18 +169,30 @@ ScreensPanel::setup_sensitivity ()
 }
 
 
+void
+ScreensPanel::convert_to_lower(string& s)
+{
+       transform(s.begin(), s.end(), s.begin(), ::tolower);
+}
+
+
+bool
+ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
+{
+       if (search.empty()) {
+               return true;
+       }
+
+       return _collator.find(search, cinema->name);
+}
+
+
 optional<wxTreeListItem>
 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
 {
-       auto search = wx_to_std (_search->GetValue ());
-       transform (search.begin(), search.end(), search.begin(), ::tolower);
-
-       if (!search.empty ()) {
-               auto name = cinema->name;
-               transform (name.begin(), name.end(), name.begin(), ::tolower);
-               if (name.find (search) == string::npos) {
-                       return {};
-               }
+       auto const search = wx_to_std(_search->GetValue());
+       if (!matches_search(cinema, search)) {
+               return {};
        }
 
        auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
@@ -231,16 +228,12 @@ ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
 void
 ScreensPanel::add_cinema_clicked ()
 {
-       auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
+       CinemaDialog dialog(GetParent(), _("Add Cinema"));
 
-       if (dialog->ShowModal() == wxID_OK) {
-               auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
+       if (dialog.ShowModal() == wxID_OK) {
+               auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
 
-               auto cinemas = Config::instance()->cinemas();
-               cinemas.sort(
-                       [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
-                       );
+               auto cinemas = sorted_cinemas();
 
                try {
                        _ignore_cinemas_changed = true;
@@ -253,8 +246,12 @@ ScreensPanel::add_cinema_clicked ()
 
                wxTreeListItem previous = wxTLI_FIRST;
                bool found = false;
+               auto const search = wx_to_std(_search->GetValue());
                for (auto existing_cinema: cinemas) {
-                       if (compare(dialog->name(), existing_cinema->name) < 0) {
+                       if (!matches_search(existing_cinema, search)) {
+                               continue;
+                       }
+                       if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
                                /* existing_cinema should be after the one we're inserting */
                                found = true;
                                break;
@@ -293,25 +290,29 @@ void
 ScreensPanel::edit_cinema_clicked ()
 {
        auto cinema = cinema_for_operation ();
-       if (!cinema) {
-               return;
+       if (cinema) {
+               edit_cinema(cinema);
        }
+}
 
-       auto dialog = new CinemaDialog(
+
+void
+ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
+{
+       CinemaDialog dialog(
                GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
                );
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
-
-       if (dialog->ShowModal() == wxID_OK) {
-               cinema->name = dialog->name();
-               cinema->emails = dialog->emails();
-               cinema->notes = dialog->notes();
-               cinema->set_utc_offset_hour(dialog->utc_offset_hour());
-               cinema->set_utc_offset_minute(dialog->utc_offset_minute());
+
+       if (dialog.ShowModal() == wxID_OK) {
+               cinema->name = dialog.name();
+               cinema->emails = dialog.emails();
+               cinema->notes = dialog.notes();
+               cinema->set_utc_offset_hour(dialog.utc_offset_hour());
+               cinema->set_utc_offset_minute(dialog.utc_offset_minute());
                notify_cinemas_changed();
                auto item = cinema_to_item(cinema);
                DCPOMATIC_ASSERT(item);
-               _targets->SetItemText (*item, std_to_wx(dialog->name()));
+               _targets->SetItemText (*item, std_to_wx(dialog.name()));
        }
 }
 
@@ -350,27 +351,26 @@ ScreensPanel::add_screen_clicked ()
                return;
        }
 
-       auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
+       ScreenDialog dialog(GetParent(), _("Add Screen"));
 
-       if (dialog->ShowModal () != wxID_OK) {
+       if (dialog.ShowModal () != wxID_OK) {
                return;
        }
 
        for (auto screen: cinema->screens()) {
-               if (screen->name == dialog->name()) {
+               if (screen->name == dialog.name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
                                        _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
-                                       std_to_wx(dialog->name()).data()
+                                       std_to_wx(dialog.name()).data()
                                        )
                                );
                        return;
                }
        }
 
-       auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
+       auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
        cinema->add_screen (screen);
        notify_cinemas_changed();
 
@@ -384,13 +384,16 @@ ScreensPanel::add_screen_clicked ()
 void
 ScreensPanel::edit_screen_clicked ()
 {
-       if (_selected_screens.size() != 1) {
-               return;
+       if (_selected_screens.size() == 1) {
+               edit_screen(_selected_screens[0]);
        }
+}
 
-       auto edit_screen = _selected_screens[0];
 
-       auto dialog = new ScreenDialog(
+void
+ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
+{
+       ScreenDialog dialog(
                GetParent(), _("Edit screen"),
                edit_screen->name,
                edit_screen->notes,
@@ -398,36 +401,35 @@ ScreensPanel::edit_screen_clicked ()
                edit_screen->recipient_file,
                edit_screen->trusted_devices
                );
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
 
-       if (dialog->ShowModal() != wxID_OK) {
+       if (dialog.ShowModal() != wxID_OK) {
                return;
        }
 
        auto cinema = edit_screen->cinema;
        for (auto screen: cinema->screens()) {
-               if (screen != edit_screen && screen->name == dialog->name()) {
+               if (screen != edit_screen && screen->name == dialog.name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
                                        _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
-                                       std_to_wx(dialog->name()).data()
+                                       std_to_wx(dialog.name()).data()
                                        )
                                );
                        return;
                }
        }
 
-       edit_screen->name = dialog->name();
-       edit_screen->notes = dialog->notes();
-       edit_screen->recipient = dialog->recipient();
-       edit_screen->recipient_file = dialog->recipient_file();
-       edit_screen->trusted_devices = dialog->trusted_devices();
+       edit_screen->name = dialog.name();
+       edit_screen->notes = dialog.notes();
+       edit_screen->recipient = dialog.recipient();
+       edit_screen->recipient_file = dialog.recipient_file();
+       edit_screen->trusted_devices = dialog.trusted_devices();
        notify_cinemas_changed();
 
        auto item = screen_to_item(edit_screen);
        DCPOMATIC_ASSERT (item);
-       _targets->SetItemText (*item, std_to_wx(dialog->name()));
+       _targets->SetItemText(*item, std_to_wx(dialog.name()));
 }
 
 
@@ -451,6 +453,10 @@ ScreensPanel::remove_screen_clicked ()
                _targets->DeleteItem(*item);
        }
 
+       /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
+        * as well.
+        */
+       selection_changed();
        notify_cinemas_changed();
 }
 
@@ -497,15 +503,23 @@ ScreensPanel::selection_changed ()
 }
 
 
-void
-ScreensPanel::add_cinemas ()
+list<shared_ptr<Cinema>>
+ScreensPanel::sorted_cinemas() const
 {
        auto cinemas = Config::instance()->cinemas();
+
        cinemas.sort(
-               [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
+               [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
                );
 
-       for (auto cinema: cinemas) {
+       return cinemas;
+}
+
+
+void
+ScreensPanel::add_cinemas ()
+{
+       for (auto cinema: sorted_cinemas()) {
                add_cinema (cinema, wxTLI_LAST);
        }
 }
@@ -667,22 +681,6 @@ ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
 }
 
 
-int
-ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
-{
-       if (_collator) {
-               UErrorCode error = U_ZERO_ERROR;
-               boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
-               u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
-               boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
-               u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
-               return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
-       } else {
-               return strcoll(utf8_a.c_str(), utf8_b.c_str());
-       }
-}
-
-
 bool
 ScreensPanel::notify_cinemas_changed()
 {
@@ -709,3 +707,18 @@ ScreensPanel::config_changed(Config::Property property)
 }
 
 
+void
+ScreensPanel::item_activated(wxTreeListEvent& ev)
+{
+       auto iter = _item_to_cinema.find(ev.GetItem());
+       if (iter != _item_to_cinema.end()) {
+               edit_cinema(iter->second);
+       } else {
+               auto iter = _item_to_screen.find(ev.GetItem());
+               if (iter != _item_to_screen.end()) {
+                       edit_screen(iter->second);
+               }
+       }
+}
+
+