summaryrefslogtreecommitdiffstats
path: root/src/server/serverapp/serverapp.cpp
blob: 7ee06e0d310d381f48818b21021ed7b49288761b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <QTranslator>
#include <QNetworkInterface>

#include "serverapp.h"

static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize);

ServerApp::ServerApp(int& argc, char** argv)
	: QApplication(argc, argv),
		_mainWindow(NULL),
		_managerOnly(false),
		_isExam(false)
{
	setOrganizationName("openslx");
	setOrganizationDomain("openslx.org");
	setApplicationName("pvsmgr");

	_arguments = parseParameters();

	loadRooms();

	// If started in manager-only mode, and there is no current room
	// after reading the config, exit right away
	if (_managerOnly && _currentRoom == "") {
		::exit(0);
		return;
	}

	// 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);

	/* Set the global path of the settings */
	QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/");
	QSharedPointer<QSettings> sys = getSettings();
	qDebug() << "System settings are in:" << sys->fileName();

	_mainWindow = new MainWindow();
}

QStringList ServerApp::parseParameters()
{
	QStringList rest;
	for (QString a : QApplication::arguments()) {
		if (a == "--manager-only") {
			_managerOnly = true;
			break;
		} else if (a.startsWith("--config=")) {
			_iniPath = a.mid(9);
		} else {
			rest << a;
		}
	}
	return rest;

}

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

void ServerApp::loadRooms()
{
	QSharedPointer<QSettings> conf = getSettings();

	if (!conf->contains("rooms")) { qDebug() << "Invalid config file (no rooms are set)!"; return; }
	QStringList rooms = conf->value("rooms").toStringList();

	for (QString roomId : rooms) {
		conf->beginGroup(roomId);
		QString roomName = conf->value("name").toString();

		/* fallback to the old format where the room id was actually just the name */
		if (roomName == "") {
			roomName = roomId;
		}
		if (!conf->contains("mgrIP")) {
			qDebug() << "Warning: Incomplete config file (room " << roomName << " needs a mgrIP)!";
		}
		QMap<QString, QPoint> clientPositions;
		// First store all room configurations in _rooms.
		int size = conf->beginReadArray("client");
		for (int j = 0; j < size; j++) {
			conf->setArrayIndex(j);
			clientPositions.insert(conf->value("ip").toString(), conf->value("pos").toPoint());
		}
		conf->endArray();

		/* read backgroundImage */
		QString image   = conf->contains("backgroundImage") ? conf->value("backgroundImage").toString() : "";
		QString mgrIP   = conf->value("mgrIP").toString();
		QString tutorIP = conf->value("tutorIP").toString();

		QSize gridSize;
		QSize clientSize(1, 1);
		/* read some other properties of the room */
		if (conf->contains("gridSize")) {
			gridSize = conf->value("gridSize").toSize();
		}
		if (conf->contains("clientSize")) {
			clientSize = conf->value("clientSize").toSize();
		}

		foreach (const QHostAddress & address, QNetworkInterface::allAddresses()) {
			if (address != QHostAddress(QHostAddress::LocalHost) && mgrIP == address.toString()) {
				qDebug("Found this ip in config.");
				_currentRoom = roomName;
			}
		}
		conf->endGroup();

		if (!gridSize.isValid()) {
			/* ok, let's choose the minimum gridSize to fit all clients */
			gridSize = minimalGridSize(clientPositions, clientSize);
			qDebug() << "had to use minimalGridSize(): = " << gridSize;

		}
		Room* r = new Room(clientPositions, gridSize, clientSize, image, tutorIP);
		qDebug() << "read new room: " << roomName << ": " << gridSize << ", " << clientSize;
		_rooms.insert(roomName, r);
	}
}

const Room* ServerApp::getCurrentRoom()
{
	if (_rooms.contains(_currentRoom)) {
		return _rooms[_currentRoom];
	} else {
		static Room* defaultRoom = NULL;
		if (defaultRoom == NULL) {
			defaultRoom = new Room(QMap<QString,
			                       QPoint>(), QSize(8, 6), QSize(1, 1), "", "");
		}
		return defaultRoom;
	}
}
void ServerApp::setSessionName(const QString& name)
{
	_sessionName = name;
	_sessionNameArray = name.toUtf8();
}

void ServerApp::setSessionName()
{
	const QString name = QString::number(qrand() % 9000 + 1000);
	_sessionName = name;
	_sessionNameArray = name.toUtf8();
}

QSharedPointer<QSettings> ServerApp::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 the minimal grid size such that all clients fit on the grid
  **/
static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize)
{
	/* collect the maximum coordinates */
	int x = 0;
	int y = 0;

	for (auto it = clientPositions.begin(); it != clientPositions.end(); ++it) {
		QPoint pos = it.value();
		if (pos.x() > x) { x = pos.x(); }
		if (pos.y() > y) { y = pos.y(); }

	}
	/* need a little extra space */
	QSize size(x + clientSize.width(), y + clientSize.height());
	return size;
}