summaryrefslogtreecommitdiff
path: root/app/pebblestoreview.cpp
blob: 011d0566fe16312f7092617dd51fd8d9c456367f (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
#include "pebblestoreview.h"
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>

PebbleStoreView::PebbleStoreView()
    : QQuickWebView()
{
    connect(this, SIGNAL(navigationRequested(QWebNavigationRequest*)), this, SLOT(onNavigationRequested(QWebNavigationRequest*)));
}


void PebbleStoreView::onNavigationRequested(QWebNavigationRequest* request)
{
    if (request->url().scheme() == "pebble") {
        if (request->url().host() == "login") {
            QUrlQuery *accessTokenFragment = new QUrlQuery(request->url().fragment());
            qDebug()<<"login"<<accessTokenFragment->queryItemValue("access_token");
            emit loginSuccess(accessTokenFragment->queryItemValue("access_token"));
        }
    }
    if (request->url().scheme() == "pebble-method-call-js-frame") {
        QString urlStr = "";

        //Basic parse error string
        QRegExp reg(".*; source was \"(.*)\";.*");
        reg.setMinimal(true);
        if (reg.indexIn(request->url().errorString()) > -1) {
            urlStr = reg.cap(1);
            reg.setPattern("method=(.*)&args=(.*)$");
            reg.setMinimal(true);
            if (reg.indexIn(urlStr) > -1) {
                QString methodStr = reg.cap(1);
                QString argsStr = QUrl::fromPercentEncoding(reg.cap(2).toUtf8());
                emit call(methodStr, argsStr);
                if (methodStr == "loadAppToDeviceAndLocker") {
                    QJsonDocument jsonResponse = QJsonDocument::fromJson(argsStr.toUtf8());
                    QJsonObject jsonObject = jsonResponse.object();
                    QJsonObject data = jsonObject.value("data").toObject();
                    qDebug()<<"download"<<data.value("title").toString()<<data.value("pbw_file").toString();
                    emit downloadPebbleApp(data.value("title").toString(), data.value("pbw_file").toString());
                }
            }
        }
    }
}