From 75891e7c90ff8ec3533a6a1e9aac66501693d561 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 19 Aug 2015 21:19:01 +0200 Subject: Improve handling of non-existing properties in `set` --- src/SettingsHandler.qml | 43 +++++++++++++++++++++++++++++++++++++++++++ src/command/commands.js | 21 +++++++++------------ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/SettingsHandler.qml b/src/SettingsHandler.qml index 8a9a862..f281da1 100644 --- a/src/SettingsHandler.qml +++ b/src/SettingsHandler.qml @@ -2,6 +2,49 @@ import QtQuick 2.0 import Qt.labs.settings 1.0 QtObject { + function getSetter(category, name) { + try { + var type = typeof eval(category + '.' + name); + + switch ( type ) { + case 'undefined': { + throw new ReferenceError(); + break; + } + case 'string': { + return function(value) { + return eval(category + '.' + name + ' = "' + value + '"'); + } + break; + } + default: { + return function(value) { + return eval(category + '.' + name + ' = ' + value); + } + break; + } + } + } + catch (exception) { + throw category + '.' + name + ' doesnt exist.'; + } + } + + function read(category, name) { + try { + var value = eval(category + '.' + name); + + if ( typeof value === 'undefined' ) { + throw new ReferenceError(); + } else { + return value; + } + } + catch (exception) { + throw category + '.' + name + ' doesnt exist.'; + } + } + property Settings window : Settings { category: "window" diff --git a/src/command/commands.js b/src/command/commands.js index d5b9787..fce509a 100644 --- a/src/command/commands.js +++ b/src/command/commands.js @@ -14,13 +14,17 @@ function execute(output, command) { notImplemented(args[0]); } } catch (exception) { - notImplemented(args[0]); + if ( exception instanceof ReferenceError ) { + notImplemented(args[0]); + } else { + output.error(exception); + } } } -function safeEval(output, code) { +function exec(output, args) { try { - var result = eval(code); + var result = eval(args.join(' ')); if ( typeof result !== 'undefined' ) { output.log(result); @@ -30,10 +34,6 @@ function safeEval(output, code) { } } -function exec(output, args) { - safeEval(output, args.join(' ')); -} - function jump(output, index) { terminalList.selectItem(index); } @@ -61,14 +61,11 @@ function ls(output) { function set(output, args) { switch ( args.length ) { case 2: { - safeEval(output, 'settings.' + args[0] + '.' + args[1]); + output.log(settings.read(args[0], args[1])); break; } case 3: { - safeEval( - output, - 'settings.' + args[0] + '.' + args[1] + ' = "' + args[2] + '"' - ); + settings.getSetter(args[0], args[1])(args[2]); break; } default: { -- cgit v1.2.3