summaryrefslogtreecommitdiff
path: root/controller.cxx
blob: d0be5fbac89b264e8eab713f20c7b230e8227644 (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
#include <QtQml>
#include <icclient.h>
#include <qicclient.hxx>
#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}
{
	engine = qobject_cast<QQmlApplicationEngine*>(parent);
	engine->load(QUrl{QStringLiteral("qrc:/main.qml")});
	connect(engine->rootObjects()[0], SIGNAL(signUp(QString)), this, SLOT(signUp(QString)));
}

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,
	[](icclient_response* response) {
		const_cast<char*>(response->data)[response->numBytes] = '\0';
		QString brand{response->data};
		icclient_free_response(response);
		shop = new Client{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()), controller, SIGNAL(pushed()));
	connect(this, &Controller::pushed, [this,window]() {
		connect(window->findChild<QObject*>("link"), SIGNAL(activated(QString)),
				this, SLOT(allProducts(QString)));
		});
}

void Controller::allProducts(QString const& brand)
{
	connect(shop, &Client::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<QObject*>("link"), "push",
				Q_ARG(QVariant, "Catalog"),
				Q_ARG(QVariant,
					QString{QString{SECURE_SERVER} % "/" % brand % "/images/"}));
	});
	shop->allProducts();
}

Controller::~Controller()
{
	if (::allProducts) delete ::allProducts;
	if (shop) delete shop;
}