Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Denesteted saveFileXml function #651

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 80 additions & 84 deletions hi_backend/backend/BackendApplicationCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,106 +1776,102 @@ void BackendCommandTarget::Actions::createRnboTemplate(BackendRootWindow* bpe)

void BackendCommandTarget::Actions::saveFileXml(BackendRootWindow * bpe)
{
if (PresetHandler::showYesNoWindow("Save XML", "This will save the current XML file"))
if (!PresetHandler::showYesNoWindow("Save XML", "This will save the current XML file"))
return;

bpe->owner->getUserPresetHandler().initDefaultPresetManager({});

const String mainSynthChainId = bpe->owner->getMainSynthChain()->getId();
const bool hasDefaultName = mainSynthChainId == "Master Chain";

if (hasDefaultName)
{
bpe->owner->getUserPresetHandler().initDefaultPresetManager({});

const String mainSynthChainId = bpe->owner->getMainSynthChain()->getId();
const bool hasDefaultName = mainSynthChainId == "Master Chain";

if (!hasDefaultName)
{
File f = GET_PROJECT_HANDLER(bpe->getMainSynthChain()).getSubDirectory(ProjectHandler::SubDirectories::XMLPresetBackups).getChildFile(mainSynthChainId + ".xml");
debugToConsole(bpe->owner->getMainSynthChain(), "This project has never been saved as XML, please create XML first");
Actions::saveFileAsXml(bpe);
return;
}

File f = GET_PROJECT_HANDLER(bpe->getMainSynthChain()).getSubDirectory(ProjectHandler::SubDirectories::XMLPresetBackups).getChildFile(mainSynthChainId + ".xml");

if (GET_PROJECT_HANDLER(bpe->getMainSynthChain()).isActive())
{
if (f.existsAsFile())
{
if (PresetHandler::showYesNoWindow("Overwrite " + mainSynthChainId, "Overwrite the existing XML?"))
{
ValueTree v = bpe->owner->getMainSynthChain()->exportAsValueTree();
if (!GET_PROJECT_HANDLER(bpe->getMainSynthChain()).isActive())
return;

if (!f.existsAsFile())
{
debugToConsole(bpe->owner->getMainSynthChain(), "Master Chain name is not default but no corresponding XML found, please create XML first");
Actions::saveFileAsXml(bpe);
return;
}

if (!PresetHandler::showYesNoWindow("Overwrite " + mainSynthChainId, "Overwrite the existing XML?"))
return;

ValueTree v = bpe->owner->getMainSynthChain()->exportAsValueTree();
v.setProperty("BuildVersion", BUILD_SUB_VERSION, nullptr);

auto xml = v.createXml();

XmlBackupFunctions::removeEditorStatesFromXml(*xml);

Processor::Iterator<ModulatorSampler> siter(bpe->getMainSynthChain());

v.setProperty("BuildVersion", BUILD_SUB_VERSION, nullptr);

auto xml = v.createXml();
while (auto s = siter.getNextProcessor())
{
if (s->getSampleMap()->hasUnsavedChanges())
s->getSampleMap()->saveAndReloadMap();
}

XmlBackupFunctions::removeEditorStatesFromXml(*xml);
File originalScriptDirectory = XmlBackupFunctions::getScriptDirectoryFor(bpe->getMainSynthChain());

Processor::Iterator<ModulatorSampler> siter(bpe->getMainSynthChain());

while (auto s = siter.getNextProcessor())
{
if (s->getSampleMap()->hasUnsavedChanges())
s->getSampleMap()->saveAndReloadMap();
}

File originalScriptDirectory = XmlBackupFunctions::getScriptDirectoryFor(bpe->getMainSynthChain());

File scriptDirectory = originalScriptDirectory.getSiblingFile("TempScriptDirectory");

Processor::Iterator<JavascriptProcessor> iter(bpe->getMainSynthChain());

scriptDirectory.deleteRecursively();

scriptDirectory.createDirectory();

String interfaceId = "";

while (JavascriptProcessor *sp = iter.getNextProcessor())
{
if (sp->isConnectedToExternalFile())
continue;
File scriptDirectory = originalScriptDirectory.getSiblingFile("TempScriptDirectory");

Processor::Iterator<JavascriptProcessor> iter(bpe->getMainSynthChain());

String content;
scriptDirectory.deleteRecursively();
scriptDirectory.createDirectory();

String interfaceId = "";

if (auto jmp = dynamic_cast<JavascriptMidiProcessor*>(sp))
{
if (jmp->isFront())
interfaceId = jmp->getId();
}
while (JavascriptProcessor *sp = iter.getNextProcessor())
{
if (sp->isConnectedToExternalFile())
continue;

sp->mergeCallbacksToScript(content);
String content;

File scriptFile = XmlBackupFunctions::getScriptFileFor(bpe->getMainSynthChain(), scriptDirectory, dynamic_cast<Processor*>(sp)->getId());
if (auto jmp = dynamic_cast<JavascriptMidiProcessor*>(sp))
{
if (jmp->isFront())
interfaceId = jmp->getId();
}

scriptFile.replaceWithText(content);
}
sp->mergeCallbacksToScript(content);

XmlBackupFunctions::removeAllScripts(*xml);
File scriptFile = XmlBackupFunctions::getScriptFileFor(bpe->getMainSynthChain(), scriptDirectory, dynamic_cast<Processor*>(sp)->getId());

if(interfaceId.isNotEmpty())
XmlBackupFunctions::extractContentData(*xml, interfaceId, f);
scriptFile.replaceWithText(content);
}

XmlBackupFunctions::removeAllScripts(*xml);

f.replaceWithText(xml->createDocument(""));
if(interfaceId.isNotEmpty())
XmlBackupFunctions::extractContentData(*xml, interfaceId, f);

debugToConsole(bpe->owner->getMainSynthChain(), "Save " + mainSynthChainId + " to " + f.getFullPathName());

if (originalScriptDirectory.deleteRecursively())
{
scriptDirectory.moveFileTo(originalScriptDirectory);
}
else
{
PresetHandler::showMessageWindow("Error at writing script file",
"The embedded script files could not be saved (probably because the file is opened somewhere else).\nPress OK to show the folder and move it manually", PresetHandler::IconType::Error);
f.replaceWithText(xml->createDocument(""));

debugToConsole(bpe->owner->getMainSynthChain(), "Save " + mainSynthChainId + " to " + f.getFullPathName());

scriptDirectory.revealToUser();
}
}
}
else
{
debugToConsole(bpe->owner->getMainSynthChain(), "Master Chain name is not default but no corresponding XML found, please create XML first");
Actions::saveFileAsXml(bpe);
}
}
}
else
{
debugToConsole(bpe->owner->getMainSynthChain(), "This project has never been saved as XML, please create XML first");
Actions::saveFileAsXml(bpe);
}
if (originalScriptDirectory.deleteRecursively())
{
scriptDirectory.moveFileTo(originalScriptDirectory);
}
else
{
PresetHandler::showMessageWindow("Error at writing script file",
"The embedded script files could not be saved (probably because the file is opened somewhere else).\nPress OK to show the folder and move it manually", PresetHandler::IconType::Error);

scriptDirectory.revealToUser();
}
}

void BackendCommandTarget::Actions::saveFileAsXml(BackendRootWindow * bpe)
Expand Down