diff options
| author | Javier <dev.git@javispedro.com> | 2014-11-30 21:32:13 +0100 |
|---|---|---|
| committer | Javier <dev.git@javispedro.com> | 2014-11-30 21:32:13 +0100 |
| commit | dadca6f0b1e4660876cccb51702998d378a5dc03 (patch) | |
| tree | d445720615174a49ec79dbe5bb3817778a6451df /daemon | |
| parent | 2e0e33bd2d588a96fc471d024de583ec7d287f5e (diff) | |
convert appinfo into a Q_GADGET with properties
Diffstat (limited to 'daemon')
| -rw-r--r-- | daemon/appinfo.cpp | 142 | ||||
| -rw-r--r-- | daemon/appinfo.h | 66 | ||||
| -rw-r--r-- | daemon/appmanager.cpp | 34 | ||||
| -rw-r--r-- | daemon/appmanager.h | 18 | ||||
| -rw-r--r-- | daemon/daemon.pro | 6 | ||||
| -rw-r--r-- | daemon/jskitmanager.cpp | 31 | ||||
| -rw-r--r-- | daemon/jskitmanager.h | 2 |
7 files changed, 258 insertions, 41 deletions
diff --git a/daemon/appinfo.cpp b/daemon/appinfo.cpp new file mode 100644 index 0000000..a4442a3 --- /dev/null +++ b/daemon/appinfo.cpp @@ -0,0 +1,142 @@ +#include "appinfo.h" +#include <QSharedData> + +struct AppInfoData : public QSharedData { + QUuid uuid; + QString shortName; + QString longName; + QString companyName; + int versionCode; + QString versionLabel; + bool watchface; + bool jskit; + QHash<QString, int> appKeys; + QString path; +}; + +AppInfo::AppInfo() : d(new AppInfoData) +{ + d->versionCode = 0; + d->watchface = false; + d->jskit = false; +} + +AppInfo::AppInfo(const AppInfo &rhs) : d(rhs.d) +{ +} + +AppInfo &AppInfo::operator=(const AppInfo &rhs) +{ + if (this != &rhs) + d.operator=(rhs.d); + return *this; +} + +AppInfo::~AppInfo() +{ +} + +QUuid AppInfo::uuid() const +{ + return d->uuid; +} + +void AppInfo::setUuid(const QUuid &uuid) +{ + d->uuid = uuid; +} + +QString AppInfo::shortName() const +{ + return d->shortName; +} + +void AppInfo::setShortName(const QString &string) +{ + d->shortName = string; +} + +QString AppInfo::longName() const +{ + return d->longName; +} + +void AppInfo::setLongName(const QString &string) +{ + d->longName = string; +} + +QString AppInfo::companyName() const +{ + return d->companyName; +} + +void AppInfo::setCompanyName(const QString &string) +{ + d->companyName = string; +} + +int AppInfo::versionCode() const +{ + return d->versionCode; +} + +void AppInfo::setVersionCode(int code) +{ + d->versionCode = code; +} + +QString AppInfo::versionLabel() const +{ + return d->versionLabel; +} + +void AppInfo::setVersionLabel(const QString &string) +{ + d->versionLabel = string; +} + +bool AppInfo::isWatchface() const +{ + return d->watchface; +} + +void AppInfo::setWatchface(bool b) +{ + d->watchface = b; +} + +bool AppInfo::isJSKit() const +{ + return d->jskit; +} + +void AppInfo::setJSKit(bool b) +{ + d->jskit = b; +} + +QHash<QString, int> AppInfo::appKeys() const +{ + return d->appKeys; +} + +void AppInfo::setAppKeys(const QHash<QString, int> &appKeys) +{ + d->appKeys = appKeys; +} + +void AppInfo::addAppKey(const QString &key, int value) +{ + d->appKeys.insert(key, value); +} + +QString AppInfo::path() const +{ + return d->path; +} + +void AppInfo::setPath(const QString &string) +{ + d->path = string; +} diff --git a/daemon/appinfo.h b/daemon/appinfo.h new file mode 100644 index 0000000..da71dfc --- /dev/null +++ b/daemon/appinfo.h @@ -0,0 +1,66 @@ +#ifndef APPINFO_H +#define APPINFO_H + +#include <QObject> +#include <QUuid> +#include <QHash> +#include <QSharedDataPointer> + +class AppInfoData; + +class AppInfo +{ + Q_GADGET + + Q_PROPERTY(QUuid uuid READ uuid WRITE setUuid) + Q_PROPERTY(QString shortName READ shortName WRITE setShortName) + Q_PROPERTY(QString longName READ longName WRITE setLongName) + Q_PROPERTY(QString companyName READ companyName WRITE setCompanyName) + Q_PROPERTY(int versionCode READ versionCode WRITE setVersionCode) + Q_PROPERTY(QString versionLabel READ versionLabel WRITE setVersionLabel) + Q_PROPERTY(bool watchface READ isWatchface WRITE setWatchface) + Q_PROPERTY(bool jskit READ isJSKit WRITE setJSKit) + Q_PROPERTY(QString path READ path WRITE setPath) + +public: + AppInfo(); + AppInfo(const AppInfo &); + AppInfo &operator=(const AppInfo &); + ~AppInfo(); + + QUuid uuid() const; + void setUuid(const QUuid &uuid); + + QString shortName() const; + void setShortName(const QString &string); + + QString longName() const; + void setLongName(const QString &string); + + QString companyName() const; + void setCompanyName(const QString &string); + + int versionCode() const; + void setVersionCode(int code); + + QString versionLabel() const; + void setVersionLabel(const QString &string); + + bool isWatchface() const; + void setWatchface(bool b); + + bool isJSKit() const; + void setJSKit(bool b); + + QHash<QString, int> appKeys() const; + void setAppKeys(const QHash<QString, int> &string); + void addAppKey(const QString &key, int value); + + QString path() const; + void setPath(const QString &string); + +private: + QSharedDataPointer<AppInfoData> d; +}; + +#endif // APPINFO_H diff --git a/daemon/appmanager.cpp b/daemon/appmanager.cpp index d06681e..867a15e 100644 --- a/daemon/appmanager.cpp +++ b/daemon/appmanager.cpp @@ -29,12 +29,12 @@ QStringList AppManager::appPaths() const QStandardPaths::LocateDirectory); } -const AppManager::AppInfo & AppManager::info(const QUuid &uuid) const +AppInfo AppManager::info(const QUuid &uuid) const { return _apps.value(uuid); } -const AppManager::AppInfo & AppManager::info(const QString &name) const +AppInfo AppManager::info(const QString &name) const { QUuid uuid = _names.value(name); if (!uuid.isNull()) { @@ -98,30 +98,32 @@ void AppManager::scanApp(const QString &path) const QJsonObject root = doc.object(); AppInfo info; - info.uuid = QUuid(root["uuid"].toString()); - info.shortName = root["shortName"].toString(); - info.longName = root["longName"].toString(); - info.company = root["companyName"].toString(); - info.versionCode = root["versionCode"].toInt(); - info.versionLabel = root["versionLabel"].toString(); + info.setUuid(QUuid(root["uuid"].toString())); + info.setShortName(root["shortName"].toString()); + info.setLongName(root["longName"].toString()); + info.setCompanyName(root["companyName"].toString()); + info.setVersionCode(root["versionCode"].toInt()); + info.setVersionLabel(root["versionLabel"].toString()); const QJsonObject watchapp = root["watchapp"].toObject(); - info.isWatchface = watchapp["watchface"].toBool(); - info.isJSKit = appDir.exists("pebble-js-app.js"); + info.setWatchface(watchapp["watchface"].toBool()); + info.setJSKit(appDir.exists("pebble-js-app.js")); const QJsonObject appkeys = root["appKeys"].toObject(); for (QJsonObject::const_iterator it = appkeys.constBegin(); it != appkeys.constEnd(); ++it) { - info.appKeys.insert(it.key(), it.value().toInt()); + info.addAppKey(it.key(), it.value().toInt()); } - if (info.uuid.isNull() || info.shortName.isEmpty()) { + info.setPath(path); + + if (info.uuid().isNull() || info.shortName().isEmpty()) { logger()->warn() << "invalid or empty uuid/name in" << appInfoFile.fileName(); return; } - _apps.insert(info.uuid, info); - _names.insert(info.shortName, info.uuid); + _apps.insert(info.uuid(), info); + _names.insert(info.shortName(), info.uuid()); - const char *type = info.isWatchface ? "watchface" : "app"; - logger()->debug() << "found installed" << type << info.shortName << info.versionLabel << "with uuid" << info.uuid.toString(); + const char *type = info.isWatchface() ? "watchface" : "app"; + logger()->debug() << "found installed" << type << info.shortName() << info.versionLabel() << "with uuid" << info.uuid().toString(); } diff --git a/daemon/appmanager.h b/daemon/appmanager.h index dc2a979..7458e19 100644 --- a/daemon/appmanager.h +++ b/daemon/appmanager.h @@ -6,6 +6,7 @@ #include <QUuid> #include <QFileSystemWatcher> #include <Log4Qt/Logger> +#include "appinfo.h" class AppManager : public QObject { @@ -15,23 +16,10 @@ class AppManager : public QObject public: explicit AppManager(QObject *parent = 0); - struct AppInfo { - QUuid uuid; - QString shortName; - QString longName; - QString company; - int versionCode; - QString versionLabel; - bool isWatchface; - bool isJSKit; - QHash<QString, int> appKeys; - QString path; - }; - QStringList appPaths() const; - const AppInfo & info(const QUuid &uuid) const; - const AppInfo & info(const QString &shortName) const; + AppInfo info(const QUuid &uuid) const; + AppInfo info(const QString &shortName) const; public slots: void rescan(); diff --git a/daemon/daemon.pro b/daemon/daemon.pro index 21c15c8..5338bfd 100644 --- a/daemon/daemon.pro +++ b/daemon/daemon.pro @@ -26,7 +26,8 @@ SOURCES += \ datalogmanager.cpp \ unpacker.cpp \ appmsgmanager.cpp \ - jskitmanager.cpp + jskitmanager.cpp \ + appinfo.cpp HEADERS += \ manager.h \ @@ -43,7 +44,8 @@ HEADERS += \ datalogmanager.h \ appmsgmanager.h \ jskitmanager.h \ - jskitmanager_p.h + jskitmanager_p.h \ + appinfo.h OTHER_FILES += \ org.pebbled.xml \ diff --git a/daemon/jskitmanager.cpp b/daemon/jskitmanager.cpp index 698b22b..41451ac 100644 --- a/daemon/jskitmanager.cpp +++ b/daemon/jskitmanager.cpp @@ -1,4 +1,4 @@ -#include <QQmlEngine> +#include <QFile> #include <QJSValueIterator> #include "jskitmanager.h" #include "jskitmanager_p.h" @@ -28,9 +28,9 @@ JSKitManager::~JSKitManager() void JSKitManager::handleAppStarted(const QUuid &uuid) { - const auto &info = _apps->info(uuid); - if (!info.uuid.isNull() && info.isJSKit) { - logger()->debug() << "Preparing to start JSKit app" << info.uuid << info.shortName; + AppInfo info = _apps->info(uuid); + if (!info.uuid().isNull() && info.isJSKit()) { + logger()->debug() << "Preparing to start JSKit app" << info.uuid() << info.shortName(); _curApp = info; startJsApp(); } @@ -38,21 +38,29 @@ void JSKitManager::handleAppStarted(const QUuid &uuid) void JSKitManager::handleAppStopped(const QUuid &uuid) { - if (!_curApp.uuid.isNull()) { - if (_curApp.uuid != uuid) { + if (!_curApp.uuid().isNull()) { + if (_curApp.uuid() != uuid) { logger()->warn() << "Closed app with invalid UUID"; } - _curApp = AppManager::AppInfo(); + stopJsApp(); + _curApp.setUuid(QUuid()); // Clear the uuid to force invalid app } } void JSKitManager::startJsApp() { if (_engine) stopJsApp(); + if (_curApp.uuid().isNull()) { + logger()->warn() << "Attempting to start JS app with invalid UUID"; + return; + } + _engine = new QJSEngine(this); _jspebble = new JSKitPebble(this); + logger()->debug() << "starting JS app"; + QJSValue globalObj = _engine->globalObject(); globalObj.setProperty("Pebble", _engine->newQObject(_jspebble)); @@ -62,12 +70,21 @@ void JSKitManager::startJsApp() it.next(); logger()->debug() << "JS property:" << it.name(); } + + QFile scriptFile(_curApp.path() + "/pebble-js-app.js"); + if (!scriptFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + logger()->warn() << "Failed to open JS file at:" << scriptFile.fileName(); + stopJsApp(); + return; + } } void JSKitManager::stopJsApp() { if (!_engine) return; // Nothing to do! + logger()->debug() << "stopping JS app"; + _engine->collectGarbage(); delete _engine; diff --git a/daemon/jskitmanager.h b/daemon/jskitmanager.h index 5e2880f..2f5ae42 100644 --- a/daemon/jskitmanager.h +++ b/daemon/jskitmanager.h @@ -33,7 +33,7 @@ private: AppManager *_apps; AppMsgManager *_appmsg; - AppManager::AppInfo _curApp; + AppInfo _curApp; QJSEngine *_engine; QPointer<JSKitPebble> _jspebble; }; |
