diff options
Diffstat (limited to 'src/server/serverapp/serverapp.cpp')
-rw-r--r-- | src/server/serverapp/serverapp.cpp | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/src/server/serverapp/serverapp.cpp b/src/server/serverapp/serverapp.cpp index 2cc237a..c91ed15 100644 --- a/src/server/serverapp/serverapp.cpp +++ b/src/server/serverapp/serverapp.cpp @@ -1,15 +1,16 @@ +#include "../mainwindow/mainwindow.h" +#include "serverapp.h" +#include "../../shared/util.h" + #include <QTranslator> #include <QNetworkInterface> - -#include "serverapp.h" +#include <QSettings> +#include <QLibraryInfo> static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize); ServerApp::ServerApp(int& argc, char** argv) - : QApplication(argc, argv), - _mainWindow(nullptr), - _managerOnly(false), - _isExam(false) + : QApplication(argc, argv) { setOrganizationName("openslx"); setOrganizationDomain("openslx.org"); @@ -21,20 +22,20 @@ ServerApp::ServerApp(int& argc, char** argv) // If started in manager-only mode, and there is no current room // after reading the config, exit right away - if (_managerOnly && _currentRoom == "") { - ::exit(0); + if (_managerOnly && _currentRoom.isEmpty()) { + _doExit = true; return; } // System strings - QTranslator *qtTranslator = new QTranslator(this); + auto *qtTranslator = new QTranslator(this); if (!qtTranslator->load(QLocale::system(), "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { qDebug() << "Loading system translations failed" << QLibraryInfo::location(QLibraryInfo::TranslationsPath); } else { installTranslator(qtTranslator); } // App specific - QTranslator *translator = new QTranslator(this); + auto *translator = new QTranslator(this); if (!translator->load(QLocale::system(), ":", "l_")) { qDebug() << "Loading app translations failed"; } else { @@ -43,20 +44,20 @@ ServerApp::ServerApp(int& argc, char** argv) /* Set the global path of the settings */ QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/"); - QSharedPointer<QSettings> sys = getSettings(); + QSettings* sys = getSettings(); qDebug() << "System settings are in:" << sys->fileName(); - _mainWindow = new MainWindow(); + new MainWindow(); } QStringList ServerApp::parseParameters() { QStringList rest; - for (QString a : QApplication::arguments()) { - if (a == "--manager-only") { + for (const QString& a : QApplication::arguments()) { + if (a == QStringLiteral("--manager-only")) { _managerOnly = true; break; - } else if (a.startsWith("--config=")) { + } else if (a.startsWith(QStringLiteral("--config="))) { _iniPath = a.mid(9); } else { rest << a; @@ -73,17 +74,20 @@ QStringList ServerApp::arguments() void ServerApp::loadRooms() { - QSharedPointer<QSettings> conf = getSettings(); + QSettings* conf = getSettings(); - if (!conf->contains("rooms")) { qDebug() << "Invalid config file (no rooms are set)!"; return; } + if (!conf->contains(QStringLiteral("rooms"))) { + qDebug() << "Invalid config file (no rooms are set)!"; + return; + } QStringList rooms = conf->value("rooms").toStringList(); - for (QString roomId : rooms) { + for (const 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 == "") { + if (roomName.isEmpty()) { roomName = roomId; } if (!conf->contains("mgrIP")) { @@ -114,8 +118,8 @@ void ServerApp::loadRooms() } foreach (const QHostAddress & address, QNetworkInterface::allAddresses()) { - if (address != QHostAddress(QHostAddress::LocalHost) && mgrIP == address.toString()) { - qDebug("Found this ip in config."); + if (!address.isBroadcast() && !address.isLoopback() && !address.isMulticast() && mgrIP == address.toString()) { + qDebug() << "Found own ip in config."; _currentRoom = roomName; } } @@ -133,18 +137,17 @@ void ServerApp::loadRooms() } } -const Room* ServerApp::getCurrentRoom() +const Room* ServerApp::getCurrentRoom() const { - if (_rooms.contains(_currentRoom)) { - return _rooms[_currentRoom]; - } else { - static Room* defaultRoom = nullptr; - if (defaultRoom == nullptr) { - defaultRoom = new Room(QMap<QString, - QPoint>(), QSize(8, 6), QSize(1, 1), "", ""); - } - return defaultRoom; + auto *room = _rooms.value(_currentRoom); + if (room != nullptr) + return room; + static Room* defaultRoom = nullptr; + if (defaultRoom == nullptr) { + defaultRoom = new Room(QMap<QString, + QPoint>(), QSize(8, 6), QSize(1, 1), "", "<none>"); } + return defaultRoom; } void ServerApp::setSessionName(const QString& name) { @@ -154,20 +157,20 @@ void ServerApp::setSessionName(const QString& name) void ServerApp::setSessionName() { - const QString name = QString::number(qrand() % 9000 + 1000); + const QString name = QString::number(slxrand() % 9000 + 1000); _sessionName = name; _sessionNameArray = name.toUtf8(); } -QSharedPointer<QSettings> ServerApp::getSettings() +QSettings * ServerApp::getSettings() { - QSharedPointer<QSettings> set; - if (_iniPath == "") { + QSettings *set; + if (_iniPath.isEmpty()) { /* default location (system scope) */ - set = QSharedPointer<QSettings>(new QSettings(QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2")); + set = new QSettings(QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2", this); } else { /* use _iniPath to find ini file */ - set = QSharedPointer<QSettings>(new QSettings(_iniPath, QSettings::IniFormat)); + set = new QSettings(_iniPath, QSettings::IniFormat, this); } set->setIniCodec("UTF-8"); return set; @@ -182,14 +185,15 @@ static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize 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(); } - + for (const auto &pos : clientPositions) { + 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; + return QSize(x + clientSize.width(), y + clientSize.height()); } |