#include #include #include #include "controller.hxx" static QQmlApplicationEngine* engine; static QString* certFile = nullptr; static Controller* controller; using namespace QInterchange; static Interchange* shop = nullptr; static Catalog* allProducts = nullptr; extern "C" { void sign_up(char const*, char const*, void (*)(interchange_response*)); struct interchange_catalog* catalog(char const*, char const*); } Controller::Controller(QObject* parent) : QObject{parent} { engine = qobject_cast(parent); engine->load(QUrl{QStringLiteral("qrc:/main.qml")}); connect(engine->rootObjects()[0], SIGNAL(signUp(QString)), this, SLOT(signUp(QString))); } Controller::~Controller() { if (::allProducts) delete ::allProducts; if (shop) delete shop; } void 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 controller = this; sign_up(brand.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr, [](interchange_response* response) { const_cast(response->data)[response->numBytes] = '\0'; QString brand{response->data}; interchange_free_response(response); shop = new Interchange{QString{QString{SECURE_SERVER} % "/" % brand}.toLatin1().constData(), QString{"/" % brand % "/images"}.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr}; if (certFile) delete certFile; controller->link(brand); }); } void Controller::link(QString const& brand) { auto window = engine->rootObjects()[0]; QMetaObject::invokeMethod(window, "push", Q_ARG(QVariant, "Link"), Q_ARG(QVariant, brand)); connect(window, SIGNAL(pushed()), this, SIGNAL(pushed())); connect(this, &Controller::pushed, [this,window]() { connect(window->findChild("link"), SIGNAL(activated(QString)), this, SLOT(allProducts(QString))); }); } void Controller::allProducts(QString const& brand) { connect(shop, &Interchange::gotCatalog, [brand](QString const& response) { ::allProducts = new Catalog{catalog(response.toLatin1().constData(), QString{"/" % brand % "/images"}.toLatin1().constData())}; engine->rootContext()->setContextProperty("catalog", ::allProducts); QMetaObject::invokeMethod(engine->rootObjects()[0]->findChild("link"), "push", Q_ARG(QVariant, "Catalog"), Q_ARG(QVariant, QString{QString{SECURE_SERVER} % "/" % brand % "/images/"})); }); shop->allProducts(); }