summaryrefslogtreecommitdiff
path: root/controller.cxx
blob: 390f8c255f5a41ad22c6659e55c5a2be6e821dff (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
#include <QStringBuilder>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <qrtclient/user.hxx>
#include "controller.hxx"

Controller::Controller(QObject* parent) : QObject{parent}
{
	auto dir = QDir{QStandardPaths::writableLocation(
			QStandardPaths::AppDataLocation)};
	auto dirPath = dir.absolutePath();
#ifdef __ANDROID__
	QString cert{CA_BUNDLE};
	QString certPath{dirPath % cert.remove(0, cert.lastIndexOf("/"))};
	QFile{"assets:" % cert}.copy(certPath);
#else
	dir.mkpath(dirPath);
#endif
	rt = new Client{SERVER_URL,
		QString{dirPath % "/cookies.txt"}.toLatin1().constData()
#ifdef __ANDROID__
		, certPath.toLatin1().constData()
#endif
	};
	ticketList = nullptr;
	historyList = nullptr;
	auto engine = static_cast<QQmlApplicationEngine*>(parent);
	engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
	auto context = engine->rootContext();
	auto window = engine->rootObjects()[0];
	connect(window, SIGNAL(logIn(QString, QString)),
			rt, SLOT(logIn(QString, QString)));
	connect(rt, &Client::loggedIn,
			rt, static_cast<void (Client::*)(QString const&)>
			(&Client::userShow));
	connect(rt, &Client::userShown, [this](User const& user) {
			rt->searchTicket(user.name());
		});
	connect(rt, &Client::searchedTicket,
		[this,context,window](TicketList const& list) {
			ticketList = new TicketList{list};
			context->setContextProperty("ticketList", ticketList);
			QMetaObject::invokeMethod(window, "pushHome");
		});
	connect(window, SIGNAL(ticketHistoryList(int, bool)),
			rt, SLOT(ticketHistoryList(int, bool)));
	connect(rt, &Client::gotTicketHistoryList,
		[this,context,window](TicketHistoryList const& list) {
			historyList = new TicketHistoryList{list};
			context->setContextProperty("historyList", historyList);
			QMetaObject::invokeMethod(window, "pushTicketHistory");
		});
	connect(window, SIGNAL(ticketNew(QString, QString)),
			rt, SLOT(ticketNew(QString, QString)));
}

Controller::~Controller()
{
	if (historyList) delete historyList;
	if (ticketList) delete ticketList;
	if (rt) delete rt;
}