diff --git a/include/effectengine/EffectDefinition.h b/include/effectengine/EffectDefinition.h index 3877ef09..1212e2e3 100644 --- a/include/effectengine/EffectDefinition.h +++ b/include/effectengine/EffectDefinition.h @@ -6,7 +6,6 @@ struct EffectDefinition { - QString name; - QString script; + QString name, script, file; QJsonObject args; }; diff --git a/libsrc/effectengine/EffectEngine.cpp b/libsrc/effectengine/EffectEngine.cpp index eebc508c..fe4cb817 100644 --- a/libsrc/effectengine/EffectEngine.cpp +++ b/libsrc/effectengine/EffectEngine.cpp @@ -248,6 +248,7 @@ bool EffectEngine::loadEffectDefinition(const QString &path, const QString &effe // ---------- setup the definition ---------- + effectDefinition.file = fileName; QJsonObject config = configEffect.object(); QString scriptName = config["script"].toString(); effectDefinition.name = config["name"].toString(); diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 0bd96f2f..70cd97bb 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -285,6 +286,8 @@ void JsonClientConnection::handleMessage(const QString& messageString) handleEffectCommand(message, command, tan); else if (command == "create-effect") handleCreateEffectCommand(message, command, tan); + else if (command == "delete-effect") + handleDeleteEffectCommand(message, command, tan); else if (command == "serverinfo") handleServerInfoCommand(message, command, tan); else if (command == "clear") @@ -481,7 +484,15 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, effectJson["name"] = message["name"].toString(); effectJson["script"] = message["script"].toString(); effectJson["args"] = message["args"].toObject(); - QJsonFactory::writeJson(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json"), effectJson); + + QFileInfo newFileName(effectArray[0].toString() + QDir::separator() + message["name"].toString().replace(QString(" "), QString("")) + QString(".json")); + + while(newFileName.exists()) + { + newFileName.setFile(effectArray[0].toString() + QDir::separator() + newFileName.baseName() + QString::number(qrand() % ((10) - 0) + 0) + QString(".json")); + } + + QJsonFactory::writeJson(newFileName.absoluteFilePath(), effectJson); } else { sendErrorReply("Can't save new effect. Effect path empty", command, tan); @@ -497,6 +508,43 @@ void JsonClientConnection::handleCreateEffectCommand(const QJsonObject& message, sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan); } +void JsonClientConnection::handleDeleteEffectCommand(const QJsonObject& message, const QString& command, const int tan) +{ + struct find_effect: std::unary_function + { + QString effectName; + find_effect(QString effectName) :effectName(effectName) { } + bool operator()(EffectDefinition const& effectDefinition) const + { + return effectDefinition.name == effectName; + } + }; + + if(message.size() > 0) + { + QString effectName = message["name"].toString(); + std::list effectsDefinition = _hyperion->getEffects(); + std::list::iterator it = std::find_if(effectsDefinition.begin(), effectsDefinition.end(), find_effect(effectName)); + + if (it != effectsDefinition.end()) + { + QFileInfo effectConfigurationFile(it->file); + if (effectConfigurationFile.absoluteFilePath().mid(0, 1) != ":" ) + { + if (effectConfigurationFile.exists()) + { + bool result = QFile::remove(effectConfigurationFile.absoluteFilePath()); + (result) ? sendSuccessReply(command, tan) : sendErrorReply("Can't delete effect configuration file: " + effectConfigurationFile.absoluteFilePath() + ". Please check permissions", command, tan); + } else + sendErrorReply("Can't find effect configuration file: " + effectConfigurationFile.absoluteFilePath(), command, tan); + } else + sendErrorReply("Can't delete internal effect: " + message["name"].toString(), command, tan); + } else + sendErrorReply("Effect " + message["name"].toString() + " not found", command, tan); + } else + sendErrorReply("Error while parsing json: Message size " + QString(message.size()), command, tan); +} + void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QString& command, const int tan) { // create result @@ -649,6 +697,7 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt { QJsonObject effect; effect["name"] = effectDefinition.name; + effect["file"] = effectDefinition.file; effect["script"] = effectDefinition.script; effect["args"] = effectDefinition.args; effects.append(effect); diff --git a/libsrc/jsonserver/JsonClientConnection.h b/libsrc/jsonserver/JsonClientConnection.h index 52698cf7..268c3f08 100644 --- a/libsrc/jsonserver/JsonClientConnection.h +++ b/libsrc/jsonserver/JsonClientConnection.h @@ -96,6 +96,13 @@ private: /// void handleCreateEffectCommand(const QJsonObject & message, const QString &command, const int tan); + /// + /// Handle an incoming JSON Effect message (Delete JSON Effect) + /// + /// @param message the incoming message + /// + void handleDeleteEffectCommand(const QJsonObject & message, const QString &command, const int tan); + /// /// Handle an incoming JSON Server info message /// diff --git a/libsrc/jsonserver/JsonSchemas.qrc b/libsrc/jsonserver/JsonSchemas.qrc index 513fc305..56359910 100644 --- a/libsrc/jsonserver/JsonSchemas.qrc +++ b/libsrc/jsonserver/JsonSchemas.qrc @@ -12,6 +12,7 @@ schema/schema-adjustment.json schema/schema-effect.json schema/schema-create-effect.json + schema/schema-delete-effect.json schema/schema-sourceselect.json schema/schema-config.json schema/schema-componentstate.json diff --git a/libsrc/jsonserver/schema/schema-delete-effect.json b/libsrc/jsonserver/schema/schema-delete-effect.json new file mode 100644 index 00000000..8279f854 --- /dev/null +++ b/libsrc/jsonserver/schema/schema-delete-effect.json @@ -0,0 +1,21 @@ + +{ + "type":"object", + "required":true, + "properties":{ + "command": { + "type" : "string", + "required" : true, + "enum" : ["delete-effect"] + }, + "tan" : { + "type" : "integer" + }, + "name" : + { + "type" : "string", + "required" : true + } + }, + "additionalProperties": false +} diff --git a/libsrc/jsonserver/schema/schema.json b/libsrc/jsonserver/schema/schema.json index 353e13ca..566c9904 100644 --- a/libsrc/jsonserver/schema/schema.json +++ b/libsrc/jsonserver/schema/schema.json @@ -5,7 +5,7 @@ "command": { "type" : "string", "required" : true, - "enum" : ["color", "image", "effect", "create-effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"] + "enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "transform", "correction", "temperature", "adjustment", "sourceselect", "config", "componentstate", "ledcolors"] } } } diff --git a/src/hyperion-remote/JsonConnection.cpp b/src/hyperion-remote/JsonConnection.cpp index 50ba7eb7..fd11f69d 100644 --- a/src/hyperion-remote/JsonConnection.cpp +++ b/src/hyperion-remote/JsonConnection.cpp @@ -209,6 +209,22 @@ void JsonConnection::createEffect(const QString &effectName, const QString &effe parseReply(reply); } +void JsonConnection::deleteEffect(const QString &effectName) +{ + qDebug() << "Delete effect configuration" << effectName; + + // create command + QJsonObject effect; + effect["command"] = QString("delete-effect"); + effect["name"] = effectName; + + // send command message + QJsonObject reply = sendMessage(effect); + + // parse reply message + parseReply(reply); +} + QString JsonConnection::getServerInfo() { qDebug() << "Get server info"; diff --git a/src/hyperion-remote/JsonConnection.h b/src/hyperion-remote/JsonConnection.h index 2e0469a6..4b545fb5 100644 --- a/src/hyperion-remote/JsonConnection.h +++ b/src/hyperion-remote/JsonConnection.h @@ -64,6 +64,13 @@ public: /// @param effectArgs The arguments of the effect /// void createEffect(const QString &effectName, const QString &effectScript, const QString & effectArgs); + + /// + /// Delete a effect configuration file (.json) + /// + /// @param effectName The name of the effect + /// + void deleteEffect(const QString &effectName); /// /// Retrieve a list of all occupied priority channels diff --git a/src/hyperion-remote/hyperion-remote.cpp b/src/hyperion-remote/hyperion-remote.cpp index de971552..34f0e787 100644 --- a/src/hyperion-remote/hyperion-remote.cpp +++ b/src/hyperion-remote/hyperion-remote.cpp @@ -65,6 +65,7 @@ int main(int argc, char * argv[]) Option & argEffectFile = parser.add