- auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
- if (d->ShowModal () == wxID_OK) {
- auto c = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
- Config::instance()->add_cinema (c);
- auto id = add_cinema (c);
- if (id) {
+ auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
+ ScopeGuard sg = [dialog]() { dialog->Destroy(); };
+
+ 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 = sorted_cinemas();
+
+ 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;
+ }
+
+ 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;
+ break;
+ }
+ auto item = cinema_to_item(existing_cinema);
+ DCPOMATIC_ASSERT(item);
+ previous = *item;
+ }
+
+ auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
+
+ if (item) {