From 9f479b8f76238a03bce5d13aee14efd34e659c6e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sun, 30 Oct 2022 20:34:23 +0100 Subject: Clean up and modernize code - static "new-style" signal->slot connections - Fix a lot of things Clang-Tidy complained about - Move includes to .cpp files and use forward decls in .h - Don't use and , but specific includes instead --- src/server/serverapp/serverapp.cpp | 85 ++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 41 deletions(-) (limited to 'src/server/serverapp/serverapp.cpp') diff --git a/src/server/serverapp/serverapp.cpp b/src/server/serverapp/serverapp.cpp index 2cc237a..933517b 100644 --- a/src/server/serverapp/serverapp.cpp +++ b/src/server/serverapp/serverapp.cpp @@ -1,15 +1,15 @@ #include #include +#include +#include #include "serverapp.h" +#include "../../shared/util.h" static QSize minimalGridSize(const QMap& 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"); @@ -22,19 +22,19 @@ 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); + QApplication::exit(0); 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 +43,20 @@ ServerApp::ServerApp(int& argc, char** argv) /* Set the global path of the settings */ QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/"); - QSharedPointer 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 +73,20 @@ QStringList ServerApp::arguments() void ServerApp::loadRooms() { - QSharedPointer 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 +117,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 +136,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(), 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(), QSize(8, 6), QSize(1, 1), "", ""); } + return defaultRoom; } void ServerApp::setSessionName(const QString& name) { @@ -154,20 +156,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 ServerApp::getSettings() +QSettings * ServerApp::getSettings() { - QSharedPointer set; - if (_iniPath == "") { + QSettings *set; + if (_iniPath.isEmpty()) { /* default location (system scope) */ - set = QSharedPointer(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(new QSettings(_iniPath, QSettings::IniFormat)); + set = new QSettings(_iniPath, QSettings::IniFormat, this); } set->setIniCodec("UTF-8"); return set; @@ -182,14 +184,15 @@ static QSize minimalGridSize(const QMap& 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()); } -- cgit v1.2.3-55-g7522