diff options
-rw-r--r-- | Catalog.qml | 2 | ||||
-rw-r--r-- | controller.cxx | 75 | ||||
-rw-r--r-- | controller.hxx | 6 | ||||
-rw-r--r-- | handler.c | 2 | ||||
-rw-r--r-- | main.qml | 2 | ||||
-rw-r--r-- | registration.c | 29 |
6 files changed, 52 insertions, 64 deletions
diff --git a/Catalog.qml b/Catalog.qml index be144c5..a004ee9 100644 --- a/Catalog.qml +++ b/Catalog.qml @@ -17,7 +17,7 @@ Rectangle { previewImage { width: 64 height: 64 - source: imageDir + "thumb/" + image + source: imageBase + "thumb/" + image } nameLabel.text: description priceLabel.text: price diff --git a/controller.cxx b/controller.cxx index 6f7759f..8354974 100644 --- a/controller.cxx +++ b/controller.cxx @@ -1,67 +1,66 @@ -#include <icclient.h> #include <QtQml> -#include <qicclient/admin.hxx> +#include <icclient.h> +#include <qicclient.hxx> #include "controller.hxx" -extern "C" { - void sign_up(char const*, char const*, void (*)(icclient_response *)); - struct icclient_catalog* catalog_data(char const*, char const*); -} - static Controller* controller; static QQmlApplicationEngine* engine; -static QICClient::Client* interchange = nullptr; -static QString* path = nullptr; -static QString* imageDir = nullptr; -static Catalog* catalog = nullptr; +static QString* certFile = nullptr; +using namespace QICClient; +static Client* shop = nullptr; +static Catalog* allProducts = nullptr; + +extern "C" { + void sign_up(char const*, char const*, void (*)(icclient_response*)); + struct icclient_catalog* catalog(char const*, char const*); +} Controller::Controller(QObject* parent) : QObject{parent} { controller = this; -#ifdef __ANDROID__ - QString cert{CA_BUNDLE}; - path = new QString{QDir{ - QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)}.absolutePath() - % cert.remove(0, cert.lastIndexOf("/"))}; - QFile{"assets:" % cert}.copy(*path); -#endif engine = static_cast<QQmlApplicationEngine*>(parent); engine->load(QUrl{QStringLiteral("qrc:/main.qml")}); auto window = engine->rootObjects()[0]; connect(window, SIGNAL(signUp(QString)), this, SIGNAL(signUp(QString))); connect(this, &Controller::signUp, [](QString const& brand) { - sign_up(brand.toLatin1().constData(), path ? path->toLatin1().constData() : nullptr, +#ifdef __ANDROID__ + QString certAsset{CA_BUNDLE}; + certFile = new QString{QDir{ + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + }.absolutePath() % certAsset.remove(0, certAsset.lastIndexOf("/"))}; + QFile{"assets:" % certAsset}.copy(*certFile); +#endif + sign_up(brand.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr, [](icclient_response* response) { QString brand{response->data}; icclient_free_response(response); QString sampleUrl{QString{SECURE_SERVER} % "/" % brand}; - imageDir = new QString{"/" % brand % "/images"}; - interchange = new Client{sampleUrl.toLatin1().constData(), - imageDir->toLatin1().constData() -#ifdef __ANDROID__ - , path->toLatin1().constData() -#endif + QString imageDir{"/" % brand % "/images"}; + shop = new Client{ + sampleUrl.toLatin1().constData(), + imageDir.toLatin1().constData(), + certFile ? certFile->toLatin1().constData() : nullptr }; - delete path; + if (certFile) delete certFile; auto window = engine->rootObjects()[0]; - window->setProperty("imageDir", QString{sampleUrl % "/images/"}); - controller->connect(interchange, &Client::gotCatalog, - [window](QString const& response) { - catalog = new Catalog{ - catalog_data(response.toLatin1().constData(), - imageDir->toLatin1().constData())}; + window->setProperty("imageBase", QString{sampleUrl % "/images/"}); + controller->connect(shop, &Client::gotCatalog, + [imageDir,window](QString const& response) { + allProducts = new Catalog{catalog( + response.toLatin1().constData(), + imageDir.toLatin1().constData())}; engine->rootContext()->setContextProperty("catalog", - catalog); + allProducts); QMetaObject::invokeMethod(window, "pushCatalog"); }); - interchange->allProducts(); - }); + shop->allProducts(); + } + ); }); } Controller::~Controller() { - if (catalog) delete catalog; - if (imageDir) delete imageDir; - if (interchange) delete interchange; + if (allProducts) delete allProducts; + if (shop) delete shop; } diff --git a/controller.hxx b/controller.hxx index 845179b..b2733fc 100644 --- a/controller.hxx +++ b/controller.hxx @@ -1,9 +1,7 @@ #ifndef CONTROLLER_HXX #define CONTROLLER_HXX -#include <qicclient.hxx> - -using namespace QICClient; +#include <QObject> class Controller : public QObject { @@ -12,7 +10,7 @@ class Controller : public QObject Controller(QObject* parent = nullptr); ~Controller(); signals: - void signUp(QString const& name); + void signUp(QString const& brand); }; #endif @@ -50,7 +50,7 @@ static void recurse_catalog(TidyDoc doc, TidyNode tnod, const char *image_dir, } } -struct icclient_catalog *catalog_data(const char *response, const char *image_dir) +struct icclient_catalog *catalog(const char *response, const char *image_dir) { TidyDoc tdoc = tidyCreate(); TidyBuffer output = {0}; @@ -3,7 +3,7 @@ import QtQuick.Controls 2.15 import "larva/features" ApplicationWindow { - property string imageDir + property string imageBase signal signUp(string brand) function pushCatalog() { stack.push("Catalog.qml") diff --git a/registration.c b/registration.c index 5e0fe00..5f21f74 100644 --- a/registration.c +++ b/registration.c @@ -1,28 +1,21 @@ +#include <stdlib.h> +#include <string.h> +#include <icclient.h> + #ifdef DEBUG #ifdef __ANDROID__ #include <android/log.h> #else #include <stdio.h> #endif -#ifdef __EMSCRIPTEN__ -#include <emscripten/fetch.h> -#else -#include <threads.h> -#include <curl/curl.h> #endif -#include <stdlib.h> -#include <string.h> -#include <icclient/typedefs.h> #ifdef __EMSCRIPTEN__ - -static void clean_up(struct emscripten_fetch_t *fetch) -{ - free(fetch->userData); - emscripten_fetch_close(fetch); -} +#include <emscripten/fetch.h> #else +#include <threads.h> +#include <curl/curl.h> struct container { CURL *curl; @@ -65,24 +58,22 @@ static int async(void *arg) } #endif -#endif - void sign_up(const char *brand, const char *certificate, void (*handler)(icclient_response *)) { char *data = malloc(strlen(brand) + 1); strcpy(data, brand); #ifdef __EMSCRIPTEN__ + (void)certificate; emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; strcpy(attr.requestMethod, "POST"); + attr.userData = data; attr.requestData = data; attr.requestDataSize = strlen(data); - attr.userData = data; attr.onsuccess = handler; - attr.onerror = clean_up; + attr.onerror = icclient_free_response; emscripten_fetch(&attr, "registration"); - (void)certificate; #else curl_global_init(CURL_GLOBAL_SSL); CURL *curl = curl_easy_init(); |