From 1b0b32be3b8eb690243d92612b5aa00d9d2d4fa9 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle Date: Sun, 3 Nov 2024 15:09:26 +0100 Subject: [PATCH] #3150 scripting: start string-secret settings type implementation Signed-off-by: Patrizio Bekerle --- docs/scripting/examples/variables.qml | 6 ++++++ src/services/scriptingservice.cpp | 25 +++++++++++++++++++++---- src/widgets/scriptsettingwidget.cpp | 17 ++++++++++++++++- src/widgets/scriptsettingwidget.h | 2 +- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/docs/scripting/examples/variables.qml b/docs/scripting/examples/variables.qml index 8bb665dc6c..e1736d684d 100644 --- a/docs/scripting/examples/variables.qml +++ b/docs/scripting/examples/variables.qml @@ -28,6 +28,12 @@ Script { "type": "string", "default": "My default value", }, + { + "identifier": "myStringSecret", + "name": "I am a password field", + "description": "Please enter a valid string:", + "type": "string-secret", + }, { "identifier": "myBoolean", "name": "I am a checkbox", diff --git a/src/services/scriptingservice.cpp b/src/services/scriptingservice.cpp index 97f366a060..bd1a4ce918 100644 --- a/src/services/scriptingservice.cpp +++ b/src/services/scriptingservice.cpp @@ -43,6 +43,7 @@ QT_WARNING_DISABLE_GCC("-Wmismatched-new-delete") #include #include +#include "cryptoservice.h" #include "openaiservice.h" #include "widgets/qownnotesmarkdowntextedit.h" #endif @@ -200,6 +201,22 @@ QList ScriptingService::registerSettingsVariables(QObject *object, con } object->setProperty(identifier.toUtf8(), value); + } else if (type == QStringLiteral("string-secret")) { + QString value = jsonObject.value(identifier).toString().toUtf8().toBase64(); + + qDebug() << __func__ << " - 'type': " << type; + qDebug() << __func__ << " - 'value': " << value; + qDebug() << __func__ << " - 'CryptoService::instance()->encryptToString(value)': " + << CryptoService::instance()->encryptToString(value); + + // if (jsonObject.value(identifier).isUndefined()) { + // value = variableMap[QStringLiteral("default")].toString(); + // } + + +// object->setProperty(identifier.toUtf8(), value); + object->setProperty(identifier.toUtf8(), + CryptoService::instance()->encryptToString(value)); } else { QString value = jsonObject.value(identifier).toString(); @@ -2405,8 +2422,8 @@ void ScriptingService::onScriptThreadDone(ScriptThread *thread) { * @return {QString} the cache dir path */ QString ScriptingService::cacheDir(const QString &subDir) const { - QString cacheDir = - QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/scripts/"); + QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + QStringLiteral("/scripts/"); if (!subDir.isEmpty()) { cacheDir = QDir::toNativeSeparators(cacheDir + subDir); @@ -2427,8 +2444,8 @@ QString ScriptingService::cacheDir(const QString &subDir) const { * @return {bool} true on success */ bool ScriptingService::clearCacheDir(const QString &subDir) const { - QString cacheDir = - QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/scripts/"); + QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + QStringLiteral("/scripts/"); if (!subDir.isEmpty()) { cacheDir = QDir::toNativeSeparators(cacheDir + subDir); diff --git a/src/widgets/scriptsettingwidget.cpp b/src/widgets/scriptsettingwidget.cpp index 34094d0f8f..1b180516b8 100644 --- a/src/widgets/scriptsettingwidget.cpp +++ b/src/widgets/scriptsettingwidget.cpp @@ -7,9 +7,10 @@ #include #include +#include "services/cryptoservice.h" #include "ui_scriptsettingwidget.h" -ScriptSettingWidget::ScriptSettingWidget(QWidget *parent, Script script, +ScriptSettingWidget::ScriptSettingWidget(QWidget *parent, const Script &script, QMap variableMap) : QWidget(parent), ui(new Ui::ScriptSettingWidget) { ui->setupUi(this); @@ -73,6 +74,20 @@ ScriptSettingWidget::ScriptSettingWidget(QWidget *parent, Script script, } ui->stringLineEdit->setText(value); + ui->stringLineEdit->setEchoMode(QLineEdit::Normal); + ui->stringLineEdit->show(); + } else if (type == "string-secret") { + QString value = CryptoService::instance()->decryptToString( + QByteArray::fromBase64(jsonObject.value(identifier).toString().toUtf8())); +// QString value = CryptoService::instance()->decryptToString( +// jsonObject.value(identifier).toString()); + + // if (jsonObject.value(identifier).isUndefined()) { + // value = variableMap["default"].toString(); + // } + + ui->stringLineEdit->setText(value); + ui->stringLineEdit->setEchoMode(QLineEdit::Password); ui->stringLineEdit->show(); } else if (type == "text") { QString value = jsonObject.value(identifier).toString(); diff --git a/src/widgets/scriptsettingwidget.h b/src/widgets/scriptsettingwidget.h index 9513faff48..720622f7b5 100644 --- a/src/widgets/scriptsettingwidget.h +++ b/src/widgets/scriptsettingwidget.h @@ -16,7 +16,7 @@ class ScriptSettingWidget : public QWidget { Q_OBJECT public: - explicit ScriptSettingWidget(QWidget *parent, Script script, + explicit ScriptSettingWidget(QWidget *parent, const Script &script, QMap variableMap); ~ScriptSettingWidget();