Skip to content

Commit

Permalink
#3150 scripting: start string-secret settings type implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
  • Loading branch information
pbek committed Nov 3, 2024
1 parent ec29170 commit 1b0b32b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/scripting/examples/variables.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 21 additions & 4 deletions src/services/scriptingservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ QT_WARNING_DISABLE_GCC("-Wmismatched-new-delete")
#include <QInputDialog>
#include <QMessageBox>

#include "cryptoservice.h"
#include "openaiservice.h"
#include "widgets/qownnotesmarkdowntextedit.h"
#endif
Expand Down Expand Up @@ -200,6 +201,22 @@ QList<QVariant> 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();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion src/widgets/scriptsettingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <QJsonObject>
#include <limits>

#include "services/cryptoservice.h"
#include "ui_scriptsettingwidget.h"

ScriptSettingWidget::ScriptSettingWidget(QWidget *parent, Script script,
ScriptSettingWidget::ScriptSettingWidget(QWidget *parent, const Script &script,
QMap<QString, QVariant> variableMap)
: QWidget(parent), ui(new Ui::ScriptSettingWidget) {
ui->setupUi(this);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/scriptsettingwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString, QVariant> variableMap);
~ScriptSettingWidget();

Expand Down

0 comments on commit 1b0b32b

Please sign in to comment.