void DialogStyleManager::OnCurrentImport() { auto filename = OpenFileSelector(_("Open subtitles file"), "Path/Last/Subtitles", "", "", SubtitleFormat::GetWildcards(0), this); if (filename.empty()) return; std::string charset; try { charset = CharSetDetect::GetEncoding(filename); } catch (agi::UserCancelException const&) { return; } AssFile temp; try { auto reader = SubtitleFormat::GetReader(filename, charset); if (!reader) wxMessageBox("Unsupported subtitle format", "Error", wxOK | wxICON_ERROR | wxCENTER, this); else reader->ReadFile(&temp, filename, charset); } catch (agi::Exception const& err) { wxMessageBox(to_wx(err.GetChainedMessage()), "Error", wxOK | wxICON_ERROR | wxCENTER, this); } catch (...) { wxMessageBox("Unknown error", "Error", wxOK | wxICON_ERROR | wxCENTER, this); return; } // Get styles auto styles = temp.GetStyles(); if (styles.empty()) { wxMessageBox(_("The selected file has no available styles."), _("Error Importing Styles")); return; } // Get selection wxArrayInt selections; int res = GetSelectedChoices(this, selections, _("Choose styles to import:"), _("Import Styles"), to_wx(styles)); if (res == -1 || selections.empty()) return; bool modified = false; // Loop through selection for (auto const& sel : selections) { // Check if there is already a style with that name if (AssStyle *existing = c->ass->GetStyle(styles[sel])) { int answer = wxMessageBox( wxString::Format(_("There is already a style with the name \"%s\" in the current script. Overwrite?"), styles[sel]), _("Style name collision"), wxYES_NO); if (answer == wxYES) { modified = true; *existing = *temp.GetStyle(styles[sel]); } continue; } // Copy modified = true; c->ass->InsertLine(temp.GetStyle(styles[sel])->Clone()); } // Update if (modified) c->ass->Commit(_("style import"), AssFile::COMMIT_STYLES); }
void AssStyleStorage::ReplaceIntoFile(AssFile &file) { for (auto const& s : style) { delete file.GetStyle(s->name); file.Styles.push_back(*new AssStyle(*s)); } }