diff options
| author | Tomasz Sterna <tomek@xiaoka.com> | 2015-01-11 00:08:59 +0100 |
|---|---|---|
| committer | Tomasz Sterna <tomek@xiaoka.com> | 2015-01-11 00:08:59 +0100 |
| commit | f92941ec79d8ddaf772a7f1e21600c447af87ecd (patch) | |
| tree | 4c2ccf5d7dd9eb753c03a8ab40e63b518a4e5ed5 | |
| parent | c1230f9c89218e4c47a21aac20f2593fbd751224 (diff) | |
Moved all Pebble App file access to AppInfo
| -rw-r--r-- | daemon/appinfo.cpp | 61 | ||||
| -rw-r--r-- | daemon/appinfo.h | 6 | ||||
| -rw-r--r-- | daemon/bankmanager.cpp | 24 |
3 files changed, 51 insertions, 40 deletions
diff --git a/daemon/appinfo.cpp b/daemon/appinfo.cpp index 91467b0..939b569 100644 --- a/daemon/appinfo.cpp +++ b/daemon/appinfo.cpp @@ -179,13 +179,17 @@ QString AppInfo::getJSApp() const { if (!isValid() || !isLocal()) return QString(); - QFile file(d->path + "/pebble-js-app.js"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - qCWarning(l) << "Failed to load JS file:" << file.fileName(); + QIODevice *appJS = openFile(AppInfo::APPJS); + if (!appJS) { + qCWarning(l) << "cannot find app" << d->path << "app.js"; + return QString(); + } + if (!appJS->open(QIODevice::ReadOnly | QIODevice::Text)) { + qCWarning(l) << "cannot open app" << d->path << "app.js" << appJS->errorString(); return QString(); } - return QString::fromUtf8(file.readAll()); + return QString::fromUtf8(appJS->readAll()); } @@ -193,25 +197,29 @@ AppInfo AppInfo::fromPath(const QString &path) { AppInfo info; - QDir appDir(path); - if (!appDir.isReadable()) { - qCWarning(l) << "app" << appDir.absolutePath() << "is not readable"; - return info; + QFileInfo appPath(path); + if (!appPath.isReadable()) { + qCWarning(l) << "app" << appPath.absolutePath() << "is not readable"; + return AppInfo(); } - QFile appInfoFile(path + "/appinfo.json"); - if (!appInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - qCWarning(l) << "cannot open app info file" << appInfoFile.fileName() << ":" - << appInfoFile.errorString(); - return info; + info.d->path = path; + + QIODevice *appInfoJSON = info.openFile(AppInfo::INFO); + if (!appInfoJSON) { + qCWarning(l) << "cannot find app" << path << "info json"; + return AppInfo(); + } + if (!appInfoJSON->open(QIODevice::ReadOnly | QIODevice::Text)) { + qCWarning(l) << "cannot open app" << path << "info json" << appInfoJSON->errorString(); + return AppInfo(); } QJsonParseError parseError; - QJsonDocument doc = QJsonDocument::fromJson(appInfoFile.readAll(), &parseError); + QJsonDocument doc = QJsonDocument::fromJson(appInfoJSON->readAll(), &parseError); if (parseError.error != QJsonParseError::NoError) { - qCWarning(l) << "cannot parse app info file" << appInfoFile.fileName() << ":" - << parseError.errorString(); - return info; + qCWarning(l) << "cannot parse app" << path << "info json" << parseError.errorString(); + return AppInfo(); } const QJsonObject root = doc.object(); @@ -224,7 +232,7 @@ AppInfo AppInfo::fromPath(const QString &path) const QJsonObject watchapp = root["watchapp"].toObject(); info.d->watchface = watchapp["watchface"].toBool(); - info.d->jskit = appDir.exists("pebble-js-app.js"); + info.d->jskit = appPath.exists("pebble-js-app.js"); if (root.contains("capabilities")) { const QJsonArray capabilities = root["capabilities"].toArray(); @@ -270,10 +278,8 @@ AppInfo AppInfo::fromPath(const QString &path) } } - info.d->path = path; - if (info.uuid().isNull() || info.shortName().isEmpty()) { - qCWarning(l) << "invalid or empty uuid/name in" << appInfoFile.fileName(); + qCWarning(l) << "invalid or empty uuid/name in json of" << path; return AppInfo(); } @@ -367,11 +373,16 @@ QImage AppInfo::decodeResourceImage(const QByteArray &data) const return img; } -// TODO: abstract to QIOReader -QString AppInfo::filePath(enum AppInfo::File file) const +QIODevice *AppInfo::openFile(enum AppInfo::File file) const { QString fileName; switch (file) { + case AppInfo::INFO: + fileName = "appinfo.json"; + break; + case AppInfo::APPJS: + fileName = "pebble-js-app.js"; + break; case AppInfo::BINARY: fileName = "pebble-app.bin"; break; @@ -382,8 +393,8 @@ QString AppInfo::filePath(enum AppInfo::File file) const QDir appDir(d->path); if (appDir.exists(fileName)) { - return appDir.absoluteFilePath(fileName); + return new QFile(appDir.absoluteFilePath(fileName)); } - return QString(); + return 0; } diff --git a/daemon/appinfo.h b/daemon/appinfo.h index 2048c38..2991345 100644 --- a/daemon/appinfo.h +++ b/daemon/appinfo.h @@ -24,8 +24,10 @@ public: Q_DECLARE_FLAGS(Capabilities, Capability) enum File { + INFO, BINARY, - RESOURCES + RESOURCES, + APPJS }; Q_PROPERTY(bool local READ isLocal) @@ -75,7 +77,7 @@ public: QByteArray getMenuIconPng() const; QString getJSApp() const; - QString filePath(enum File) const; + QIODevice *openFile(enum File) const; void setInvalid(); diff --git a/daemon/bankmanager.cpp b/daemon/bankmanager.cpp index 8f31959..f4bb7b3 100644 --- a/daemon/bankmanager.cpp +++ b/daemon/bankmanager.cpp @@ -78,27 +78,25 @@ bool BankManager::uploadApp(const QUuid &uuid, int slot) return false; } - qCDebug(l) << "about to install app " << info.shortName() << "into slot" << slot; + qCDebug(l) << "about to install app" << info.shortName() << "into slot" << slot; - QFile *binaryFile = new QFile(info.filePath(AppInfo::BINARY), this); + QIODevice *binaryFile = info.openFile(AppInfo::BINARY); if (!binaryFile->open(QIODevice::ReadOnly)) { - qCWarning(l) << "failed to open" << binaryFile->fileName() << ":" << binaryFile->errorString(); + qCWarning(l) << "failed to open" << info.shortName() + << "AppInfo::BINARY" << binaryFile->errorString(); delete binaryFile; return false; } qCDebug(l) << "binary file size is" << binaryFile->size(); - QFile *resourceFile = 0; - QString resourceFileName = info.filePath(AppInfo::RESOURCES); - if (!resourceFileName.isEmpty()) { - resourceFile = new QFile(resourceFileName, this); - if (!resourceFile->open(QIODevice::ReadOnly)) { - qCWarning(l) << "failed to open" << resourceFile->fileName() << ":" << resourceFile->errorString(); - delete binaryFile; - delete resourceFile; - return false; - } + QIODevice *resourceFile = info.openFile(AppInfo::RESOURCES); + if (resourceFile && !resourceFile->open(QIODevice::ReadOnly)) { + qCWarning(l) << "failed to open " << info.shortName() + << "AppInfo::RESOURCES" << resourceFile->errorString(); + delete binaryFile; + delete resourceFile; + return false; } // Mark the slot as used, but without any app, just in case. |
