summaryrefslogtreecommitdiffstats
path: root/src/client/clientapp/clientapp.cpp
blob: 962ec4212a44dd83e4cd63cab4313605eef8e466 (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "clientapp.h"
#include <QNetworkInterface>
#include "../connectwindow/connectwindow.h"
#include "../toolbar/toolbar.h"
#include "../net/serverconnection.h"

ClientApp::ClientApp(int& argc, char** argv)
	: QApplication(argc, argv), _connectionMode(ConnectionMode::None), _examMode(false), _connection(nullptr), _isManagerPc(false)
{
	/* some values */
	setOrganizationName("openslx");
	setOrganizationDomain("openslx.org");
	setApplicationName("pvsclient");
	setQuitOnLastWindowClosed(false);

	/* configuration */
	QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/");

	/* parse parameters and keep the unparsed ones */
	_arguments = parseParameters();

	/* set translator */
	// System strings
	QTranslator *qtTranslator = new QTranslator(this);
	qtTranslator->load(QLocale::system(), "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath));
	installTranslator(qtTranslator);
	// App specific
	QTranslator *translator = new QTranslator(this);
	translator->load(QLocale::system(), ":", "l_");
	installTranslator(translator);

	readIsManagerPc();

	_connectWindow = new ConnectWindow(nullptr);
	connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(connected(ServerConnection*)));
	if (_connectionMode == ConnectionMode::Auto) {
		_toolbar = new Toolbar(true); // auto connect client without session ID.
	} else if (_connectionMode == ConnectionMode::Session) {
		_toolbar = new Toolbar(_sessionName.toUtf8()); // connect client with given session ID.
	} else {
		_toolbar = new Toolbar(); // create normal client.
	}
	_toolbar->setVisible(!_examMode);

}

/* parse arguments and return a list with the unused arguments */
QStringList ClientApp::parseParameters()
{
	QStringList rest;
	for (QString a : QApplication::arguments()) {
		if (a == "--exam-mode") {
			_examMode = true;
		} else if (a == "--auto") {
			_connectionMode = ConnectionMode::Auto;
		} else if (a.startsWith("--session=")) {
			_connectionMode = ConnectionMode::Session;
			_sessionName = a.mid(10);
		} else if (a.startsWith("--config=")) {
			_iniPath = a.mid(9);
		} else {
			rest << a;
		}
	}
	return rest;
}

QStringList ClientApp::arguments()
{
	return _arguments;
}

QSharedPointer<QSettings> ClientApp::getSettings()
{
	QSharedPointer<QSettings> set;
	if (_iniPath == "") {
		/* default location (system scope) */
		set = QSharedPointer<QSettings>(new QSettings(QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2"));
	} else {
		/* use _iniPath to find ini file */
		set = QSharedPointer<QSettings>(new QSettings(_iniPath, QSettings::IniFormat));
	}
	set->setIniCodec("UTF-8");
	return set;
}

/* returns true when the pc of this client is also eligible to be a manager */
void ClientApp::readIsManagerPc()
{
	QList<Room> myRooms;
	auto conf = clientApp->getSettings();
	QStringList roomNames = conf->value("rooms").toStringList();

	/* go through all rooms and check if this client is a manager of the room. */
	for (auto roomName : roomNames) {
		conf->beginGroup(roomName);
		const QString mgrIP = conf->value("mgrIP").toString();

		foreach (const QHostAddress & address, QNetworkInterface::allAddresses()) {
			if (address.toString()  == mgrIP) {
				_isManagerPc = true;
				return;
			}
		}
		conf->endGroup();
	}
}

void ClientApp::connected(ServerConnection* connection)
{
	_connection = connection;
	connect(connection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(disconnected(ServerConnection*)));
}

void ClientApp::disconnected(ServerConnection* connection)
{
	if (connection != nullptr)
		connection->blockSignals(true);
	if (_connection == connection) {
		_connection = nullptr;
	}
}