diff options
author | Yang Gu <gyagp0@gmail.com> | 2010-10-01 01:52:26 +0800 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2010-10-13 09:42:04 -0500 |
commit | 91c570921825acee962a6cbb9387b113085013de (patch) | |
tree | 7824566259cadc59bbe00b737890221ec90b7450 | |
parent | 49b0df7d5442b063c51af805797fc6d837874153 (diff) | |
download | phonesim-91c570921825acee962a6cbb9387b113085013de.tar.gz |
Enable Qt Script
This patch is to enable Qt script so that GUI can be controlled using
JavaScript. In the meantime, D-Bus support is added for the sake of test
automation.
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/control.cpp | 90 | ||||
-rw-r--r-- | src/control.h | 35 |
3 files changed, 126 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac index 4d10494..d5d674e 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ AC_ARG_ENABLE(optimization, AC_HELP_STRING([--disable-optimization], fi ]) -PKG_CHECK_MODULES(QT, QtCore QtGui QtXml QtNetwork, dummy=yes, +PKG_CHECK_MODULES(QT, QtCore QtGui QtXml QtNetwork QtScript, dummy=yes, AC_MSG_ERROR(Qt is required)) AC_SUBST(QT_CFLAGS) AC_SUBST(QT_LIBS) diff --git a/src/control.cpp b/src/control.cpp index 9e2c1f0..c01705b 100644 --- a/src/control.cpp +++ b/src/control.cpp @@ -51,6 +51,15 @@ ControlWidget::ControlWidget(const QString &ruleFile, Control *parent) ui = new Ui_ControlBase; ui->setupUi(this); + script = new Script(this, ui); + + if (!QDBusConnection::systemBus().registerService("org.phonesim")) { + qWarning() << QDBusConnection::systemBus().lastError().message(); + exit(-1); + } + + QDBusConnection::systemBus().registerObject("/", script, QDBusConnection::ExportAllSlots); + connect(ui->hsSignalQuality, SIGNAL(valueChanged(int)), this, SLOT(sendSQ())); connect(ui->hsBatteryCharge, SIGNAL(valueChanged(int)), this, SLOT(sendBC())); connect(ui->hsBatteryCharging, SIGNAL(stateChanged(int)), this, SLOT(chargingChanged(int))); @@ -91,6 +100,7 @@ void ControlWidget::closeEvent(QCloseEvent *event) ControlWidget::~ControlWidget() { + delete script; delete ui; delete translator; } @@ -445,3 +455,83 @@ void ControlWidget::simAppAbort() { p->simAppAbort(); } + +Script::Script(QObject *obj, Ui_ControlBase *ui) : QDBusAbstractAdaptor(obj) +{ + /* Export tabs to be accessed by script */ + QScriptValue qsTab = engine.newQObject(ui->tab); + engine.globalObject().setProperty("tabRegistration", qsTab); + + QScriptValue qsTab2 = engine.newQObject(ui->tab_2); + engine.globalObject().setProperty("tabCBM", qsTab2); + + QScriptValue qsTab3 = engine.newQObject(ui->tab_3); + engine.globalObject().setProperty("tabSMS", qsTab3); + + QScriptValue qsTab4 = engine.newQObject(ui->tab_4); + engine.globalObject().setProperty("tabVoiceMail", qsTab4); + + QScriptValue qsTab5 = engine.newQObject(ui->tab_5); + engine.globalObject().setProperty("tabUSSD", qsTab5); + + QScriptValue qsTab6 = engine.newQObject(ui->tab_6); + engine.globalObject().setProperty("tabSIM", qsTab6); +} + +void Script::SetPath(const QString &path, const QDBusMessage &msg) +{ + QDir dir(path); + if (!dir.exists()) { + QDBusMessage reply = msg.createErrorReply("org.phonesim.Error.PathNotFound", "Path doesn't exist"); + QDBusConnection::systemBus().send(reply); + return; + } + + dirPath = path; +} + +QString Script::GetPath() +{ + return dirPath; +} + +QString Script::Run(const QString &name, const QDBusMessage &msg) +{ + QString fileName; + + if (dirPath.endsWith('/')) + fileName = dirPath + name; + else + fileName = dirPath + "/" + name; + + QFile scriptFile(fileName); + + if (!scriptFile.open(QIODevice::ReadOnly)) { + QDBusMessage reply = msg.createErrorReply("org.phonesim.Error.FileNotFound", "Script file doesn't exist"); + QDBusConnection::systemBus().send(reply); + return QString(); + } + + QTextStream stream(&scriptFile); + stream.setCodec("UTF-8"); + QString contents = stream.readAll(); + scriptFile.close(); + + QScriptValue qsScript = engine.evaluate(contents); + if (qsScript.isError()) { + QString info = fileName + ", line " + qsScript.property("lineNumber").toString() + ", " + qsScript.toString(); + QDBusMessage reply = msg.createErrorReply("org.phonesim.Error.ScriptExecError", info); + QDBusConnection::systemBus().send(reply); + return QString(); + } + + /* + * If this QScriptValue is an object, calling this function has side effects on the script engine, + * since the engine will call the object's toString() function (and possibly valueOf()) in an attempt + * to convert the object to a primitive value (possibly resulting in an uncaught script exception). + */ + if (qsScript.isObject() || qsScript.isUndefined()) + return QString(); + + return qsScript.toString(); +} diff --git a/src/control.h b/src/control.h index 95536dc..e455437 100644 --- a/src/control.h +++ b/src/control.h @@ -21,11 +21,45 @@ #define CONTROL_H #include <hardwaremanipulator.h> +#include <QtDBus/QtDBus> +#include <QtScript> #include "ui_controlbase.h" #include "attranslator.h" class Control; +class Script: public QDBusAbstractAdaptor +{ +Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.phonesim.Script") + Q_CLASSINFO("D-Bus Introspection", "" +" <interface name=\"org.phonesim.Script\">\n" +" <method name=\"SetPath\">\n" +" <arg direction=\"in\" type=\"s\" name=\"path\"/>\n" +" </method>\n" +" <method name=\"GetPath\">\n" +" <arg direction=\"out\" type=\"s\" name=\"path\"/>\n" +" </method>\n" +" <method name=\"Run\">\n" +" <arg direction=\"in\" type=\"s\" name=\"name\"/>\n" +" <arg direction=\"out\" type=\"s\" name=\"result\"/>\n" +" </method>\n" +" </interface>\n" + "") + +public: + Script(QObject *obj, Ui_ControlBase *ui); + +public slots: + void SetPath(const QString &path, const QDBusMessage &msg); + QString GetPath(); + QString Run(const QString &name, const QDBusMessage &msg); + +private: + QString dirPath; + QScriptEngine engine; +}; + class ControlWidget : public QWidget { Q_OBJECT @@ -73,6 +107,7 @@ protected: private: Ui_ControlBase *ui; + Script *script; Control *p; AtTranslator *translator; |