- 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 = Config::instance()->cinemas();
+ cinemas.sort(
+ [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
+ );
+
+ try {
+ 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;
+ 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 (!item) {
+ item = add_cinema (cinema, wxTLI_LAST);
+ }
+
+ if (item) {