diff options
| author | Tomasz Sterna <tomek@xiaoka.com> | 2015-04-08 11:52:14 +0200 |
|---|---|---|
| committer | Tomasz Sterna <tomek@xiaoka.com> | 2015-04-09 08:45:16 +0200 |
| commit | 78d1697cd63033244304f7794cf9157029e4fdb5 (patch) | |
| tree | 3a1bdbe3d3706c34a8a7841a1790846599a45c8f /daemon/bundle.cpp | |
| parent | cbb0039fe542c0d8281601d25c04de487c84fa17 (diff) | |
Implemented firmwareUpgrade in daemon
Diffstat (limited to 'daemon/bundle.cpp')
| -rw-r--r-- | daemon/bundle.cpp | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/daemon/bundle.cpp b/daemon/bundle.cpp new file mode 100644 index 0000000..bfd4b83 --- /dev/null +++ b/daemon/bundle.cpp @@ -0,0 +1,129 @@ +#include "bundle.h" +#include <QSharedData> +#include <QScopedPointer> +#include <QDir> +#include <QFileInfo> +#include <QJsonDocument> +#include <QJsonObject> +#include <quazip/quazipfile.h> + +class BundleData : public QSharedData { +public: + bool isValid; + QString path; + QJsonObject manifest; + + BundleData() : isValid(false) {} +}; + +QLoggingCategory Bundle::l("Bundle"); + +Bundle::Bundle() : b(new BundleData) +{} + +Bundle::Bundle(const Bundle &rhs) : b(rhs.b) +{} + +Bundle &Bundle::operator=(const Bundle &rhs) +{ + if (this != &rhs) + b.operator=(rhs.b); + return *this; +} + +Bundle::~Bundle() +{} + +QString Bundle::type() const +{ + return b->manifest.value("type").toString(); +} + +QString Bundle::path() const +{ + return b->path; +} + +bool Bundle::isValid() const +{ + return b->isValid; +} + +Bundle Bundle::fromPath(const QString &path) +{ + Bundle bundle; + + QFileInfo bundlePath(path); + if (!bundlePath.isReadable()) { + qCWarning(l) << "bundle" << bundlePath.absolutePath() << "is not readable"; + return Bundle(); + } + + bundle.b->path = path; + + QScopedPointer<QIODevice> manifestJSON(bundle.openFile(Bundle::MANIFEST, QIODevice::Text)); + if (!manifestJSON) { + qCWarning(l) << "cannot find" << path << "manifest json"; + return Bundle(); + } + + QJsonParseError parseError; + QJsonDocument doc = QJsonDocument::fromJson(manifestJSON->readAll(), &parseError); + if (parseError.error != QJsonParseError::NoError) { + qCWarning(l) << "cannot parse" << path << "manifest json" << parseError.errorString(); + return Bundle(); + } + manifestJSON->close(); + bundle.b->manifest = doc.object(); + + bundle.b->isValid = true; + return bundle; +} + +QIODevice *Bundle::openFile(enum Bundle::File file, QIODevice::OpenMode mode) const +{ + QString fileName; + switch (file) { + case Bundle::MANIFEST: + fileName = "manifest.json"; + break; + case Bundle::INFO: + fileName = "appinfo.json"; + break; + case Bundle::APPJS: + fileName = "pebble-js-app.js"; + break; + case Bundle::BINARY: + fileName = b->manifest.value(type()).toObject().value("name").toString(); + break; + case Bundle::RESOURCES: + fileName = b->manifest.value("resources").toObject().value("name").toString(); + break; + } + + QIODevice *dev = 0; + QFileInfo bundlePath(path()); + if (bundlePath.isDir()) { + QDir bundleDir(path()); + if (bundleDir.exists(fileName)) { + dev = new QFile(bundleDir.absoluteFilePath(fileName)); + } + } else if (bundlePath.isFile()) { + dev = new QuaZipFile(path(), fileName); + } + + if (dev && !dev->open(QIODevice::ReadOnly | mode)) { + delete dev; + return 0; + } + + return dev; +} + +bool Bundle::fileExists(enum Bundle::File file) const +{ + QIODevice *dev = openFile(file); + bool exists = dev && dev->isOpen(); + delete dev; + return exists; +} |
