diff options
author | Simon Rettberg | 2022-10-30 20:34:23 +0100 |
---|---|---|
committer | Simon Rettberg | 2022-10-30 20:34:23 +0100 |
commit | 9f479b8f76238a03bce5d13aee14efd34e659c6e (patch) | |
tree | e320d32838202ac4604032da7a4bc3702cc304da /src/server/serverapp | |
parent | Update translation files (diff) | |
download | pvs2-9f479b8f76238a03bce5d13aee14efd34e659c6e.tar.gz pvs2-9f479b8f76238a03bce5d13aee14efd34e659c6e.tar.xz pvs2-9f479b8f76238a03bce5d13aee14efd34e659c6e.zip |
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 <QtWidgets> and <QtCore>, but specific includes instead
Diffstat (limited to 'src/server/serverapp')
-rw-r--r-- | src/server/serverapp/serverapp.cpp | 85 | ||||
-rw-r--r-- | src/server/serverapp/serverapp.h | 31 |
2 files changed, 58 insertions, 58 deletions
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 <QTranslator> #include <QNetworkInterface> +#include <QSettings> +#include <QLibraryInfo> #include "serverapp.h" +#include "../../shared/util.h" 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"); @@ -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<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 +73,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 +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<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), "", ""); } + 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<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 +184,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()); } diff --git a/src/server/serverapp/serverapp.h b/src/server/serverapp/serverapp.h index e0a1351..b2b16c5 100644 --- a/src/server/serverapp/serverapp.h +++ b/src/server/serverapp/serverapp.h @@ -1,14 +1,16 @@ #ifndef SERVERAPP_H #define SERVERAPP_H +#include "../mainwindow/mainwindow.h" + #include <QApplication> #include <QStringList> #include <QMap> -#include "../mainwindow/mainwindow.h" +class QSettings; struct Room { - Room(QMap<QString, QPoint> cPos, QSize grid, QSize client, QString image, QString tutor) : + Room(const QMap<QString, QPoint> &cPos, const QSize &grid, const QSize &client, const QString &image, const QString &tutor) : clientPositions(cPos), gridSize(grid), clientSize(client), @@ -23,12 +25,12 @@ struct Room { /* define a macro `serverApp` that can be used anywhere in the program and - * returns a reference to the current ClientApp instance */ + * returns a reference to the current ServerApp instance */ #if defined(serverApp) #undef serverApp #endif -#define serverApp (static_cast<ServerApp*>(QCoreApplication::instance())) +#define serverApp (static_cast<ServerApp*>(QCoreApplication::instance())) // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast) /* this class is supposed to (after complete refactoring) to encapsulate all * state of the application. At the moment, the state is distributed within @@ -42,15 +44,14 @@ class ServerApp : public QApplication private: QStringList _arguments; - MainWindow* _mainWindow; QString _sessionName; QByteArray _sessionNameArray; QMap<QString, Room*> _rooms; QString _currentRoom; - bool _managerOnly; - bool _isExam; + bool _managerOnly{}; + bool _isExam{}; QString _iniPath; QStringList parseParameters(); @@ -64,20 +65,16 @@ public: /* getters */ - const QString& sessionName() { return _sessionName; } - const QByteArray& sessionNameArray() { return _sessionNameArray; } - const QMap<QString, Room*> & rooms() { return _rooms; } - const QString& getCurrentRoomName() { return _currentRoom; } - const QMap<QString, Room*>& getRooms() { return _rooms; } - bool isExam() { return _isExam; } - bool isManagerOnly() { return _managerOnly; } - const Room* getCurrentRoom(); - QSharedPointer<QSettings> getSettings(); + const QString &sessionName() const { return _sessionName; } + const QByteArray &sessionNameArray() const { return _sessionNameArray; } + const QMap<QString, Room *> &getRooms() const { return _rooms; } + bool isExam() const { return _isExam; } + const Room* getCurrentRoom() const; + QSettings * getSettings(); /* setters */ void setSessionName(const QString& name); void setSessionName(); - void setIniPath(QString s) { _iniPath = s; }; void setCurrentRoom(const QString& room) { _currentRoom = room; } void setExam(bool exam) { _isExam = exam; } |