summaryrefslogtreecommitdiff
path: root/controller.cxx
blob: 2e562bf52dc66da1f74fcd3d2e2ad6c6174674c2 (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
#include <QQmlApplicationEngine>
#include <QtQml>
#include "worker.hxx"
#include "user.hxx"
#include "tasklist.hxx"
#include "controller.hxx"

Controller::Controller(QObject* parent) : QObject{parent}
{
	auto worker = new Worker;
	worker->moveToThread(&thread);
	connect(&thread, &QThread::finished, worker, &QObject::deleteLater);

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

	auto loginView = appWindow->findChild<QObject*>("login");
	connect(loginView, SIGNAL(logIn(QString, QString)),
			worker, SLOT(logIn(QString, QString)));
	connect(worker, SIGNAL(logged(rt_user*)), loginView, SLOT(pushProfile()));
	connect(loginView, SIGNAL(search(QString)), worker, SLOT(search(QString)));

	auto typeId = qmlRegisterSingletonType<User>("KelakonUser", 0, 1, "User"
			, [](QQmlEngine *engine,
				QJSEngine *scriptEngine) -> QObject* {
			Q_UNUSED(engine)
			Q_UNUSED(scriptEngine)
			return new User;
			});
	auto user = engine->singletonInstance<User*>(typeId);
	connect(worker, SIGNAL(logged(rt_user*)), user, SLOT(update(rt_user*)));

	taskList = new TaskList;
	engine->rootContext()->setContextProperty("taskList", taskList);
	connect(worker, SIGNAL(foundTasks(rt_ticketlist*))
			, taskList, SLOT(addTasks(rt_ticketlist*)));

	thread.start();
}

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