Fix crash when adding cinemas while a search is in force (#2378).
[dcpomatic.git] / src / wx / screens_panel.cc
index 272b983d90c8bf5e72c81e00c3b38ef3e9791163..1d94d1acb9f8027a571403d1811698bdbb73f2ac 100644 (file)
@@ -32,6 +32,7 @@
 
 
 using std::cout;
+using std::list;
 using std::make_pair;
 using std::make_shared;
 using std::map;
@@ -235,10 +236,7 @@ ScreensPanel::add_cinema_clicked ()
        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 _collator.compare(a->name, b->name) < 0; }
-                       );
+               auto cinemas = sorted_cinemas();
 
                try {
                        _ignore_cinemas_changed = true;
@@ -251,7 +249,12 @@ ScreensPanel::add_cinema_clicked ()
 
                wxTreeListItem previous = wxTLI_FIRST;
                bool found = false;
+               auto search = wx_to_std(_search->GetValue());
+               convert_to_lower(search);
                for (auto existing_cinema: cinemas) {
+                       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;
@@ -495,15 +498,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 _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);
        }
 }