Cleanup: add convert_to_lower() and matches_search().
[dcpomatic.git] / src / wx / screens_panel.cc
index 6fb9542526874c75e1f05c27144dc2f886eb2a3a..272b983d90c8bf5e72c81e00c3b38ef3e9791163 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;
@@ -45,6 +40,9 @@ using std::shared_ptr;
 using std::string;
 using std::vector;
 using boost::optional;
+#if BOOST_VERSION >= 106100
+using namespace boost::placeholders;
+#endif
 using namespace dcpomatic;
 
 
@@ -113,17 +111,11 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
 
        _check_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
-       _uncheck_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
+       _uncheck_all->Bind   (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, 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);
-       }
+       _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
 }
 
 
@@ -131,10 +123,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);
-       }
 }
 
 
@@ -179,18 +167,33 @@ 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 lower_case_search)
+{
+       if (lower_case_search.empty()) {
+               return true;
+       }
+
+       auto name = cinema->name;
+       convert_to_lower(name);
+       return name.find(lower_case_search) != string::npos;
+}
+
+
 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 {};
-               }
+       convert_to_lower(search);
+       if (!matches_search(cinema, search)) {
+               return {};
        }
 
        auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
@@ -234,28 +237,32 @@ ScreensPanel::add_cinema_clicked ()
 
                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; }
                        );
 
                try {
+                       _ignore_cinemas_changed = true;
+                       ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
                        Config::instance()->add_cinema(cinema);
                } catch (FileError& e) {
                        error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file.  Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
                        return;
                }
 
-               optional<wxTreeListItem> item;
+               wxTreeListItem previous = wxTLI_FIRST;
+               bool found = false;
                for (auto existing_cinema: cinemas) {
-                       if (!item && compare(dialog->name(), existing_cinema->name) < 0) {
-                               if (auto existing_item = cinema_to_item(existing_cinema)) {
-                                       item = add_cinema (cinema, *existing_item);
-                               }
+                       if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
+                               /* existing_cinema should be after the one we're inserting */
+                               found = true;
+                               break;
                        }
+                       auto item = cinema_to_item(existing_cinema);
+                       DCPOMATIC_ASSERT(item);
+                       previous = *item;
                }
 
-               if (!item) {
-                       item = add_cinema (cinema, wxTLI_LAST);
-               }
+               auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
 
                if (item) {
                        _targets->UnselectAll ();
@@ -321,6 +328,8 @@ ScreensPanel::remove_cinema_clicked ()
        }
 
        for (auto const& cinema: _selected_cinemas) {
+               _ignore_cinemas_changed = true;
+               ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
                Config::instance()->remove_cinema(cinema);
                auto item = cinema_to_item(cinema);
                DCPOMATIC_ASSERT(item);
@@ -491,7 +500,7 @@ ScreensPanel::add_cinemas ()
 {
        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) {
@@ -501,7 +510,7 @@ ScreensPanel::add_cinemas ()
 
 
 void
-ScreensPanel::search_changed ()
+ScreensPanel::clear_and_re_add()
 {
        _targets->DeleteAllItems ();
 
@@ -511,6 +520,13 @@ ScreensPanel::search_changed ()
        _screen_to_item.clear ();
 
        add_cinemas ();
+}
+
+
+void
+ScreensPanel::search_changed ()
+{
+       clear_and_re_add();
 
        _ignore_selection_change = true;
 
@@ -649,25 +665,12 @@ 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()
 {
+       _ignore_cinemas_changed = true;
+       ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
+
        try {
                Config::instance()->changed(Config::CINEMAS);
        } catch (FileError& e) {
@@ -679,3 +682,10 @@ ScreensPanel::notify_cinemas_changed()
 }
 
 
+void
+ScreensPanel::config_changed(Config::Property property)
+{
+       if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
+               clear_and_re_add();
+       }
+}