summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/bundle.cpp
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2016-02-11 23:55:16 +0100
committerAndrew Branson <andrew.branson@cern.ch>2016-02-11 23:55:16 +0100
commit29aaea2d80a9eb1715b6cddfac2d2aacf76358bd (patch)
tree012795b6bec16c72f38d33cff46324c9a0225868 /rockworkd/libpebble/bundle.cpp
launchpad ~mzanetti/rockwork/trunk r87
Diffstat (limited to 'rockworkd/libpebble/bundle.cpp')
-rw-r--r--rockworkd/libpebble/bundle.cpp151
1 files changed, 151 insertions, 0 deletions
diff --git a/rockworkd/libpebble/bundle.cpp b/rockworkd/libpebble/bundle.cpp
new file mode 100644
index 0000000..64061c8
--- /dev/null
+++ b/rockworkd/libpebble/bundle.cpp
@@ -0,0 +1,151 @@
+#include "bundle.h"
+
+#include <QVariantMap>
+#include <QFileInfo>
+#include <QDebug>
+#include <QJsonParseError>
+
+Bundle::Bundle(const QString &path):
+ m_path(path)
+{
+
+}
+
+QString Bundle::path() const
+{
+ return m_path;
+}
+
+QString Bundle::file(Bundle::FileType type, HardwarePlatform hardwarePlatform) const
+{
+ // Those two will always be in the top level dir. HardwarePlatform is irrelevant.
+ switch (type) {
+ case FileTypeAppInfo:
+ return m_path + "/appInfo.js";
+ case FileTypeJsApp:
+ return m_path + "/pebble-js-app.js";
+ default:
+ ;
+ }
+
+ // For all the others we have to find the manifest file
+ QList<QString> possibleDirs;
+
+ switch (hardwarePlatform) {
+ case HardwarePlatformAplite:
+ if (QFileInfo::exists(path() + "/aplite/")) {
+ possibleDirs.append("aplite");
+ }
+ possibleDirs.append("");
+ break;
+ case HardwarePlatformBasalt:
+ if (QFileInfo::exists(path() + "/basalt/")) {
+ possibleDirs.append("basalt");
+ }
+ possibleDirs.append("");
+ break;
+ case HardwarePlatformChalk:
+ if (QFileInfo::exists(path() + "/chalk/")) {
+ possibleDirs.append("chalk");
+ }
+ break;
+ default:
+ possibleDirs.append("");
+ ;
+ }
+
+ QString manifestFilename;
+ QString subDir;
+ foreach (const QString &dir, possibleDirs) {
+ if (QFileInfo::exists(m_path + "/" + dir + "/manifest.json")) {
+ subDir = "/" + dir;
+ manifestFilename = m_path + subDir + "/manifest.json";
+ break;
+ }
+ }
+ if (manifestFilename.isEmpty()) {
+ qWarning() << "Error finding manifest.json";
+ return QString();
+ }
+
+ // We want the manifiest file. just return it without parsing it
+ if (type == FileTypeManifest) {
+ return manifestFilename;
+ }
+
+ QFile manifest(manifestFilename);
+ if (!manifest.open(QFile::ReadOnly)) {
+ qWarning() << "Error opening" << manifestFilename;
+ return QString();
+ }
+ QJsonParseError error;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(manifest.readAll(), &error);
+ if (error.error != QJsonParseError::NoError) {
+ qWarning() << "Error parsing" << manifestFilename;
+ return QString();
+ }
+
+ QVariantMap manifestMap = jsonDoc.toVariant().toMap();
+ switch (type) {
+ case FileTypeApplication:
+ return m_path + subDir + "/" + manifestMap.value("application").toMap().value("name").toString();
+ case FileTypeResources:
+ if (manifestMap.contains("resources")) {
+ return m_path + subDir + "/" + manifestMap.value("resources").toMap().value("name").toString();
+ }
+ break;
+ case FileTypeWorker:
+ if (manifestMap.contains("worker")) {
+ return m_path + subDir + "/" + manifestMap.value("worker").toMap().value("name").toString();
+ }
+ break;
+ case FileTypeFirmware:
+ if (manifestMap.contains("firmware")) {
+ return m_path + subDir + "/" + manifestMap.value("firmware").toMap().value("name").toString();
+ }
+ break;
+ default:
+ ;
+ }
+ return QString();
+}
+
+quint32 Bundle::crc(Bundle::FileType type, HardwarePlatform hardwarePlatform) const
+{
+ switch (type) {
+ case FileTypeAppInfo:
+ case FileTypeJsApp:
+ case FileTypeManifest:
+ qWarning() << "Cannot get crc for file type" << type;
+ return 0;
+ default: ;
+ }
+
+ QFile manifest(file(FileTypeManifest, hardwarePlatform));
+ if (!manifest.open(QFile::ReadOnly)) {
+ qWarning() << "Error opening manifest file";
+ return 0;
+ }
+
+ QJsonParseError error;
+ QJsonDocument jsonDoc = QJsonDocument::fromJson(manifest.readAll(), &error);
+ if (error.error != QJsonParseError::NoError) {
+ qWarning() << "Error parsing manifest file";
+ return 0;
+ }
+
+ QVariantMap manifestMap = jsonDoc.toVariant().toMap();
+ switch (type) {
+ case FileTypeApplication:
+ return manifestMap.value("application").toMap().value("crc").toUInt();
+ case FileTypeResources:
+ return manifestMap.value("resources").toMap().value("crc").toUInt();
+ case FileTypeWorker:
+ return manifestMap.value("worker").toMap().value("crc").toUInt();
+ case FileTypeFirmware:
+ return manifestMap.value("firmware").toMap().value("crc").toUInt();
+ default:
+ ;
+ }
+ return 0;
+}