summaryrefslogtreecommitdiff
path: root/controller.cxx
blob: d096d8e070fbe75dc1ed51a3768bb094a8f0e6c3 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifdef ANDROID
#include <QStringBuilder>
#endif
#include <QQmlApplicationEngine>
#include <QtQml>
#include <qrtclient/client.hxx>
#include <qrtclient/user.hxx>
#include "controller.hxx"

Controller::Controller(QObject* parent)
	: QObject{parent}
	, m_ticketSubject{""}
{
#ifdef ANDROID
	QDir location{QStandardPaths::writableLocation(QStandardPaths
			::AppDataLocation)};
	QString path{location.absolutePath() % "/ca-certificates.crt"};
	QFile file{"assets:/certs/ca-certificates.crt"};
	file.copy(path);
#endif
	using RTClient::Client;
	auto client = new Client{"https://darapsa.co.id/rt"
#ifdef ANDROID
		, path.toLatin1().constData()
#endif
	};
	client->moveToThread(&thread);
	connect(&thread, &QThread::finished, client, &QObject::deleteLater);

	auto engine = static_cast<QQmlApplicationEngine*>(parent);
	auto rootObjects = engine->rootObjects();
	auto appWindow = rootObjects[0];

	using RTClient::User;
	auto typeId = qmlRegisterSingletonType<User>("KelakonUser", 0, 1, "User"
			, [](QQmlEngine *engine
				, QJSEngine *scriptEngine) -> QObject* {
				Q_UNUSED(engine)
				Q_UNUSED(scriptEngine)
				return new User;
			});
	auto qUser = engine->singletonInstance<User*>(typeId);

	using RTClient::TicketList;
	ticketList = new TicketList;
	engine->rootContext()->setContextProperty("ticketList", ticketList);

	connect(appWindow, SIGNAL(logIn(QString, QString))
			, client, SLOT(logIn(QString, QString)));

	connect(client, &Client::loggedIn
			, client, static_cast<void (Client::*)(QString const&)>
			(&Client::userShow));

	connect(client, &Client::userShown, [this,qUser](rtclient_user* user) {
			if (user) {
				emit checked(QString{user->name});
				qUser->update(user);
			}
		});

	connect(this, &Controller::checked, client, &Client::searchTicket);

	connect(client, &Client::searchedTicket, ticketList, &TicketList::update);

	connect(client, &Client::loggedIn, [appWindow,this]() {
			auto loginView = appWindow->findChild<QObject*>("login");
			connect(ticketList, &TicketList::updated, [loginView]() {
					QMetaObject::invokeMethod(loginView
							, "pushHome");
				});
		});

	connect(appWindow, SIGNAL(ticketId(int))
			, client, SLOT(ticketHistory(int)));

	connect(appWindow, SIGNAL(ticketSubject(QString))
			, this, SLOT(setTicketSubject(QString)));
	
	connect(client, &Client::gotTicketHistory
			, [appWindow,this](rtclient_ticket_history_list* list) {
			auto history = list->histories[0];
			QMetaObject::invokeMethod(appWindow, "ticketHistory"
					, Q_ARG(QVariant, m_ticketSubject)
					, Q_ARG(QVariant
						, QString{history->content})
					, Q_ARG(QVariant
						, QString{history->creator})
					, Q_ARG(QVariant
						, QString{asctime(history
								->created)}));
		});

	connect(appWindow, SIGNAL(ticketNew(QString, QString))
			, client, SLOT(ticketNew(QString, QString)));

	thread.start();
}

void Controller::setTicketSubject(QString const& subject)
{
	if (m_ticketSubject != subject) m_ticketSubject = subject;
}

Controller::~Controller()
{
	thread.quit();
	thread.wait();
	delete ticketList;
}