Add helper for changing config via JavaScript
This commit is contained in:
parent
5726bab292
commit
68c9f27e2a
|
@ -37,6 +37,7 @@ include(JsProviderConfig)
|
||||||
if(JS_PROVIDER)
|
if(JS_PROVIDER)
|
||||||
list(APPEND HEADER_FILES jsconsole.h)
|
list(APPEND HEADER_FILES jsconsole.h)
|
||||||
list(APPEND SRC_FILES jsconsole.cpp)
|
list(APPEND SRC_FILES jsconsole.cpp)
|
||||||
|
list(APPEND RES_FILES resources/js/js.qrc)
|
||||||
endif()
|
endif()
|
||||||
include(QtConfig)
|
include(QtConfig)
|
||||||
include(WindowsResources)
|
include(WindowsResources)
|
||||||
|
|
|
@ -799,6 +799,21 @@ QByteArray Application::editConfigViaScript() const
|
||||||
JSConsole console;
|
JSConsole console;
|
||||||
engine.globalObject().setProperty("console", engine.newQObject(&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
|
// evaluate the user provided script
|
||||||
const auto res(engine.evaluate(QString::fromUtf8(script), scriptFileName));
|
const auto res(engine.evaluate(QString::fromUtf8(script), scriptFileName));
|
||||||
if (res.isError()) {
|
if (res.isError()) {
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
<RCC>
|
||||||
|
<qresource prefix="/js">
|
||||||
|
<file>helper.js</file>
|
||||||
|
</qresource>
|
||||||
|
</RCC>
|
|
@ -1,6 +1,9 @@
|
||||||
// example script for changing configuration with syncthingctl
|
// example script for changing configuration with syncthingctl
|
||||||
// can be executed like: syncthingctl edit --script example.js
|
// 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
|
// alter some options
|
||||||
config.gui.useTLS = true;
|
config.gui.useTLS = true;
|
||||||
config.options.relaysEnabled = false;
|
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));
|
console.log("unpausing device " + (device.name ? device.name : device.deviceID));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pause folder "foo" if the folder exist
|
||||||
|
assignIfPresent(findFolder("foo"), "paused", true);
|
||||||
|
|
Loading…
Reference in New Issue