Overwrite custom created effect config when effect name is exist (#335)

* Overwrite exist Effects

* move find_schema and find_effect struct to include file
This commit is contained in:
Paulchen Panther 2016-12-16 19:48:15 +01:00 committed by redPanther
parent a724fd1535
commit b227f5a71c
2 changed files with 36 additions and 23 deletions

View File

@ -449,16 +449,6 @@ void JsonClientConnection::handleEffectCommand(const QJsonObject& message, const
void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan) void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, const QString &command, const int tan)
{ {
struct find_schema: std::unary_function<EffectSchema, bool>
{
QString pyFile;
find_schema(QString pyFile):pyFile(pyFile) { }
bool operator()(EffectSchema const& schema) const
{
return schema.pyFile == pyFile;
}
};
if(message.size() > 0) if(message.size() > 0)
{ {
if (!message["args"].toObject().isEmpty()) if (!message["args"].toObject().isEmpty())
@ -497,12 +487,27 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message,
effectJson["script"] = message["script"].toString(); effectJson["script"] = message["script"].toString();
effectJson["args"] = message["args"].toObject(); effectJson["args"] = message["args"].toObject();
QFileInfo newFileName(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json")); std::list<EffectDefinition> availableEffects = _hyperion->getEffects();
std::list<EffectDefinition>::iterator iter = std::find_if(availableEffects.begin(), availableEffects.end(), find_effect(message["name"].toString()));
QFileInfo newFileName;
if (iter != availableEffects.end())
{
newFileName.setFile(iter->file);
if (newFileName.absoluteFilePath().mid(0, 1) == ":")
{
sendErrorReply("The effect name '" + message["name"].toString() + "' is assigned to an internal effect. Please rename your effekt.", command, tan);
return;
}
} else
{
newFileName.setFile(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"));
while(newFileName.exists()) while(newFileName.exists())
{ {
newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json")); newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json"));
} }
}
QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson); QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson);
Info(_log, "Reload effect list"); Info(_log, "Reload effect list");
@ -523,16 +528,6 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message,
void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan) void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan)
{ {
struct find_effect: std::unary_function<EffectDefinition, bool>
{
QString effectName;
find_effect(QString effectName) :effectName(effectName) { }
bool operator()(EffectDefinition const& effectDefinition) const
{
return effectDefinition.name == effectName;
}
};
if(message.size() > 0) if(message.size() > 0)
{ {
QString effectName = message["name"].toString(); QString effectName = message["name"].toString();

View File

@ -72,7 +72,25 @@ namespace OPCODE {
} }
} }
struct find_schema: std::unary_function<EffectSchema, bool>
{
QString pyFile;
find_schema(QString pyFile):pyFile(pyFile) { }
bool operator()(EffectSchema const& schema) const
{
return schema.pyFile == pyFile;
}
};
struct find_effect: std::unary_function<EffectDefinition, bool>
{
QString effectName;
find_effect(QString effectName) :effectName(effectName) { }
bool operator()(EffectDefinition const& effectDefinition) const
{
return effectDefinition.name == effectName;
}
};
/// ///
/// The Connection object created by \a JsonServer when a new connection is establshed /// The Connection object created by \a JsonServer when a new connection is establshed