#include #include #include #include "controller.hxx" static Controller* controller; static QQmlApplicationEngine* engine; 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; engine = static_cast(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) { #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}; QString imageDir{"/" % brand % "/images"}; shop = new Client{ sampleUrl.toLatin1().constData(), imageDir.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr }; if (certFile) delete certFile; auto window = engine->rootObjects()[0]; 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", allProducts); QMetaObject::invokeMethod(window, "pushCatalog"); }); shop->allProducts(); } ); }); } Controller::~Controller() { if (allProducts) delete allProducts; if (shop) delete shop; }