summaryrefslogtreecommitdiff
path: root/controller.cxx
blob: 6f7759fc9c0dafe7e42327a6f92d45af0b4424e5 (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
#include <icclient.h>
#include <QtQml>
#include <qicclient/admin.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;

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,
			[](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
				};
				delete path;
				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())};
						engine->rootContext()->setContextProperty("catalog",
								catalog);
						QMetaObject::invokeMethod(window, "pushCatalog");
					});
				interchange->allProducts();
			});
	});
}

Controller::~Controller()
{
	if (catalog) delete catalog;
	if (imageDir) delete imageDir;
	if (interchange) delete interchange;
}