diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 818e72e..b7b433e 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -37,6 +37,7 @@ include(JsProviderConfig) if(JS_PROVIDER) list(APPEND HEADER_FILES jsconsole.h) list(APPEND SRC_FILES jsconsole.cpp) + list(APPEND RES_FILES resources/js/js.qrc) endif() include(QtConfig) include(WindowsResources) diff --git a/cli/application.cpp b/cli/application.cpp index 9b397cc..205d0b9 100644 --- a/cli/application.cpp +++ b/cli/application.cpp @@ -799,6 +799,21 @@ QByteArray Application::editConfigViaScript() const JSConsole console; engine.globalObject().setProperty("console", engine.newQObject(&console)); + // provide helper + QFile helperFile(":/js/helper.js"); + helperFile.open(QFile::ReadOnly); + const auto helperScript(helperFile.readAll()); + if (helperScript.isEmpty()) { + cerr << Phrases::Error << "Unable to load internal helper script." << Phrases::EndFlush; + return QByteArray(); + } + const auto helperRes(engine.evaluate(QString::fromUtf8(helperScript))); + if (helperRes.isError()) { + cerr << Phrases::Error << "Unable to evaluate internal helper script." << Phrases::End; + printError(helperRes); + return QByteArray(); + } + // evaluate the user provided script const auto res(engine.evaluate(QString::fromUtf8(script), scriptFileName)); if (res.isError()) { diff --git a/cli/resources/js/helper.js b/cli/resources/js/helper.js new file mode 100644 index 0000000..082eddf --- /dev/null +++ b/cli/resources/js/helper.js @@ -0,0 +1,36 @@ +function findFolder(id) { + var folders = config.folders; + for (var i = 0, count = folders.length; i !== count; ++i) { + var folder = folders[i]; + if (folder.id === id) { + return folder; + } + } +} + +function findDevice(name) { + var devices = config.devices; + for (var i = 0, count = devices.length; i !== count; ++i) { + var device = devices[i]; + if (device.name === name) { + return device; + } + } +} + +function findDeviceById(id) { + var devices = config.devices; + for (var i = 0, count = devices.length; i !== count; ++i) { + var device = devices[i]; + if (device.deviceID === name) { + return device; + } + } +} + +function assignIfPresent(object, property, value) { + if (!object) { + return; + } + object[property] = value; +} diff --git a/cli/resources/js/js.qrc b/cli/resources/js/js.qrc new file mode 100644 index 0000000..e79019f --- /dev/null +++ b/cli/resources/js/js.qrc @@ -0,0 +1,5 @@ + + + helper.js + + diff --git a/cli/testfiles/example.js b/cli/testfiles/example.js index 682e6e6..20677e0 100644 --- a/cli/testfiles/example.js +++ b/cli/testfiles/example.js @@ -1,6 +1,9 @@ // example script for changing configuration with syncthingctl // can be executed like: syncthingctl edit --script example.js +// the ECMAScript environment is either provided by Qt QML or Qt Script, see http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html +// additional helpers are defined in syncthingtray/cli/resources/js/helper.js + // alter some options config.gui.useTLS = true; config.options.relaysEnabled = false; @@ -25,3 +28,6 @@ for (var i = 0, count = devices.length; i !== count; ++i) { console.log("unpausing device " + (device.name ? device.name : device.deviceID)); } } + +// pause folder "foo" if the folder exist +assignIfPresent(findFolder("foo"), "paused", true);