Skip to content

Commit

Permalink
DeleteAllAliases = Lua script + supporting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cybercop23 committed Jan 16, 2025
1 parent b99acf8 commit 458568d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scripts/DeleteAllAliases.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
properties = {}

deleteem = PromptOption('This will delete all aliases. Are you sure you want to proceed?','Delete aliases','Yes','No')

if deleteem == "Yes" then
reallysure = PromptOption('Are you really sure you want to do this?', 'Confirm?', 'Delete', 'Cancel')
if reallysure == "Delete" then
result = RunCommand('deleteAllAliases', properties)
if result['res'] == 200 then
msg = ShowMessage('All aliases have been deleted!')
Log("Following Groups/Models/SubModels had aliases that were removed:")
Log(result['models'])
else
msg = ShowMessage('No aliases found.')
Log(result['msg'])
end
else
msg = ShowMessage('Smart move!')
end
else
msg = ShowMessage('Smart move!')
end
8 changes: 8 additions & 0 deletions xLights/automation/LuaRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ void LuaRunner::ShowMessage(std::string const& text) const
wxMessageBox(text, text, wxOK);
}

std::string LuaRunner::PromptOption(std::string const& question, std::string const& title, std::string const& button1, std::string const& button2) const
{
wxMessageDialog dlg(_frame, question, title, wxYES_NO | wxICON_QUESTION);
dlg.SetYesNoLabels(button1.c_str(), button2.c_str());
return dlg.ShowModal() == wxID_YES ? button1 : button2;
}

std::string LuaRunner::PromptString(std::string const& message) const
{
wxTextEntryDialog dialog(_frame, message, message);
Expand Down Expand Up @@ -128,6 +135,7 @@ bool LuaRunner::Run_Script(wxString const& filepath, std::function<void (std::st

lua.set_function("PromptSequences", &LuaRunner::PromptSequences, this);
lua.set_function("ShowMessage", &LuaRunner::ShowMessage, this);
lua.set_function("PromptOption", &LuaRunner::PromptOption, this);
lua.set_function("PromptString", &LuaRunner::PromptString, this);
lua.set_function("PromptSelection", &LuaRunner::PromptSelection, this);
lua.set_function("SplitString", &LuaRunner::SplitString, this);
Expand Down
1 change: 1 addition & 0 deletions xLights/automation/LuaRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class LuaRunner

[[nodiscard]] sol::object RunCommand(std::string const& cmd, std::map<std::string, std::string> parms, sol::this_state thislua);
void ShowMessage(std::string const& text) const;
[[nodiscard]] std::string PromptOption(std::string const& question, std::string const& title, std::string const& button1, std::string const& button2) const;
[[nodiscard]] std::string PromptString(std::string const& text) const;
[[nodiscard]] std::string PromptSelection(sol::object const& items, std::string const& message) const;
[[nodiscard]] std::pair<std::list<std::string>, bool> PromptSequences() const;
Expand Down
16 changes: 16 additions & 0 deletions xLights/automation/xLightsAutomations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,22 @@ bool xLightsFrame::ProcessAutomation(std::vector<std::string> &paths,
models = "[" + models + "]";
return sendResponse(models, "models", 200, true);

} else if (cmd == "deleteAllAliases") {
std::string models;
bool deleted = false;
for (auto m = (&AllModels)->begin(); m != (&AllModels)->end(); ++m) {
bool ret = m->second->DeleteAllAliases();
if (ret) {
models += (deleted ? ", " : "") + JSONSafe(m->first);
deleted = deleted || ret;
}
}
if (deleted) {
MarkEffectsFileDirty();
return sendResponse("\"" + models + "\"", "models", 200, true);
} else {
return sendResponse("No aliases found to delete.", "msg", 503, false);
}
} else if (cmd == "getViews") {
std::string views;
if (CurrentSeqXmlFile == nullptr) {
Expand Down
18 changes: 18 additions & 0 deletions xLights/models/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,24 @@ void Model::DeleteAlias(const std::string& alias)
}
}

bool Model::DeleteAllAliases() {
bool changed = false;
for (auto x = ModelXml->GetChildren(); x != nullptr; x = x->GetNext()) {
if (x->GetName() == "Aliases") {
ModelXml->RemoveChild(x);
changed = true;
} else if (x->GetName() == "subModel") {
for (auto sm = x->GetChildren(); sm != nullptr; sm = sm->GetNext()) {
if (sm->GetName() == "Aliases") {
x->RemoveChild(sm);
changed = true;
}
}
}
}
return changed;
}

const std::list<std::string> &Model::GetAliases() const
{
if (aliases.empty()) {
Expand Down
1 change: 1 addition & 0 deletions xLights/models/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class Model : public BaseObject
bool IsAlias(const std::string& alias, bool oldnameOnly = false) const;
void AddAlias(const std::string& alias);
void DeleteAlias(const std::string& alias);
bool DeleteAllAliases();
const std::list<std::string> &GetAliases() const;
void SetAliases(const std::list<std::string>& aliases);

Expand Down

0 comments on commit 458568d

Please sign in to comment.