summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/appdownloader.cpp
blob: acecf0f5470f815f05f31322a4f84a5b4cf09a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "appdownloader.h"
#include "watchconnection.h"
#include "watchdatareader.h"
#include "watchdatawriter.h"
#include "ziphelper.h"

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QDir>
#include <QFile>
#include <QJsonDocument>

AppDownloader::AppDownloader(const QString &storagePath, QObject *parent) :
    QObject(parent),
    m_storagePath(storagePath + "/apps/")
{
    m_nam = new QNetworkAccessManager(this);
}

void AppDownloader::downloadApp(const QString &id)
{
    QNetworkRequest request(QUrl("https://api2.getpebble.com/v2/apps/id/" + id));
    QNetworkReply *reply = m_nam->get(request);
    reply->setProperty("storeId", id);
    connect(reply, &QNetworkReply::finished, this, &AppDownloader::appJsonFetched);
}

void AppDownloader::appJsonFetched()
{
    QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
    reply->deleteLater();

    QString storeId = reply->property("storeId").toString();

    if (reply->error() != QNetworkReply::NoError) {
        qWarning() << "Error fetching App Json" << reply->errorString();
        return;
    }

    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
    if (error.error != QJsonParseError::NoError) {
        qWarning() << "Error parsing App Json" << error.errorString();
        return;
    }

    QVariantMap map = jsonDoc.toVariant().toMap();
    if (!map.contains("data") || map.value("data").toList().length() == 0) {
        qWarning() << "Unexpected json content:" << jsonDoc.toJson();
        return;
    }
    QVariantMap appMap = map.value("data").toList().first().toMap();
    QString pbwFileUrl = appMap.value("latest_release").toMap().value("pbw_file").toString();
    if (pbwFileUrl.isEmpty()) {
        qWarning() << "pbw file url empty." << jsonDoc.toJson();
        return;
    }

    QDir dir;
    dir.mkpath(m_storagePath + storeId);

    QString iconFile = appMap.value("list_image").toMap().value("144x144").toString();
    QNetworkRequest request(iconFile);
    QNetworkReply *imageReply = m_nam->get(request);
    qDebug() << "fetching image" << iconFile;
    connect(imageReply, &QNetworkReply::finished, [this, imageReply, storeId]() {
        imageReply->deleteLater();
        QString targetFile = m_storagePath + storeId + "/list_image.png";
        qDebug() << "saving image to" << targetFile;
        QFile f(targetFile);
        if (f.open(QFile::WriteOnly)) {
            f.write(imageReply->readAll());
            f.close();
        }
    });

    fetchPackage(pbwFileUrl, storeId);
}

void AppDownloader::fetchPackage(const QString &url, const QString &storeId)
{
    QNetworkRequest request(url);
    QNetworkReply *reply = m_nam->get(request);
    reply->setProperty("storeId", storeId);
    connect(reply, &QNetworkReply::finished, this, &AppDownloader::packageFetched);
}

void AppDownloader::packageFetched()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    reply->deleteLater();

    QString storeId = reply->property("storeId").toString();

    QFile f(m_storagePath + storeId + "/" + reply->request().url().fileName() + ".zip");
    if (!f.open(QFile::WriteOnly | QFile::Truncate)) {
        qWarning() << "Error opening file for writing";
        return;
    }
    f.write(reply->readAll());
    f.flush();
    f.close();

    QString zipName = m_storagePath + storeId + "/" + reply->request().url().fileName() + ".zip";

    if (!ZipHelper::unpackArchive(zipName, m_storagePath + storeId)) {
        qWarning() << "Error unpacking App zip file";
        return;
    }

    emit downloadFinished(storeId);
}