From f9d165687aac7423c86554b7e73ade99f9e1f3ea Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Mon, 26 Sep 2016 11:45:35 +0200 Subject: added the --config=FILE parameter and got rid of the SETTINGS macro. --- src/server/main.cpp | 43 ++++++++++++++++++++--------- src/server/mainwindow/mainwindow.cpp | 52 ++++++++++++++++++------------------ src/server/mainwindow/mainwindow.h | 2 +- src/server/util/global.cpp | 15 +++++++++++ src/server/util/global.h | 8 ++++++ src/server/util/util.h | 4 --- 6 files changed, 81 insertions(+), 43 deletions(-) diff --git a/src/server/main.cpp b/src/server/main.cpp index 2e5a32b..0c09f91 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -5,6 +5,21 @@ #include "util/global.h" #include "../shared/settings.h" +using std::cout; +using std::endl; + +void usage() { + cout << "USAGE pvsmgr [OPTIONS]" << endl; + cout << "OPTIONS: " << endl; + cout << "--manager-only" << endl; + cout << " pvsmgr terminates if this computer is not a manager of a room" << endl; + cout << "--config=INIFILE" << endl; + cout << " read configuration from INIFILE instead of default path (/opt/openslx/pvs2/pvs2.ini) " << endl; + cout << "--usage" << endl; + cout << " shows this message" << endl; +} + + int main(int argc, char** argv) { QApplication app(argc, argv); @@ -13,15 +28,19 @@ int main(int argc, char** argv) app.setOrganizationDomain("openslx.org"); app.setApplicationName("pvsmgr"); - qDebug() << "args are " << app.arguments() << ""; - for (QString a : app.arguments()) { - if (a == "--manager-only") { - Global::manager_only = true; - break; - } else if (!a.endsWith("pvsmgr")) { - qDebug() << "ignoring unknown argument: \"" << a << "\""; - } - } + for (QString a : app.arguments()) { + if (a == "--manager-only") { + Global::manager_only = true; + break; + } else if (a.startsWith("--config=")) { + Global::setIniPath(a.mid(9)); + } else if (a == "--usage" || a == "--help") { + usage(); + exit(0); + } else if (!a.endsWith("pvsmgr")) { + qDebug() << "ignoring unknown argument: \"" << a << "\""; + } + } QStringList supportedStyles = QStyleFactory::keys(); for (QString style : PREFERRED_STYLES) { @@ -36,9 +55,9 @@ int main(int argc, char** argv) // Set the global path of the settings QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/"); - SETTINGS(sys); - qDebug() << "System settings are in:" << sys.fileName(); - QFileInfo sysfi(sys.fileName()); + QSharedPointer sys = Global::getSettings(); + qDebug() << "System settings are in:" << sys->fileName(); + QFileInfo sysfi(sys->fileName()); // use system locale as language to translate gui diff --git a/src/server/mainwindow/mainwindow.cpp b/src/server/mainwindow/mainwindow.cpp index bed07f6..ae83dfb 100644 --- a/src/server/mainwindow/mainwindow.cpp +++ b/src/server/mainwindow/mainwindow.cpp @@ -118,9 +118,9 @@ MainWindow::MainWindow(QWidget* parent) : connect(ui->action_DeleteClient, SIGNAL(triggered()), this, SLOT(onDeleteClient())); /* In exam-mode: disable most features */ - SETTINGS(conf); - if (conf.contains("examMode")) { - Global::setExam(conf.value("examMode").toBool()); + QSharedPointer conf = Global::getSettings(); + if (conf->contains("examMode")) { + Global::setExam(conf->value("examMode").toBool()); } if (Global::isExam()) { @@ -338,11 +338,11 @@ ConnectionFrame* MainWindow::createFrame(QString computerId, QPoint pxCoord, QPo * @param y * @return If loadPosition was successfull. */ -bool MainWindow::loadPosition(QSettings& settings, const QString& id, int& x, int& y) +bool MainWindow::loadPosition(QSharedPointer settings, const QString& id, int& x, int& y) { - settings.beginGroup("client_position"); - const QVariant retval = (settings.value(id)); - settings.endGroup(); + settings->beginGroup("client_position"); + const QVariant retval = (settings->value(id)); + settings->endGroup(); if (retval.type() != QVariant::Point) return false; const QPoint point(retval.toPoint()); @@ -399,51 +399,51 @@ void MainWindow::tryToUseRoomTemplate() { qDebug() << "tryToUseRoomTemplate()"; QMap roomsList; - SETTINGS(conf); + QSharedPointer conf = Global::getSettings(); - if (!conf.contains("rooms")) { qDebug() << "Invalid config file (no rooms are set)!"; return; } - QStringList rooms = conf.value("rooms").toStringList(); + if (!conf->contains("rooms")) { qDebug() << "Invalid config file (no rooms are set)!"; return; } + QStringList rooms = conf->value("rooms").toStringList(); qDebug() << rooms; QString myRoom = ""; for (QString roomId : rooms) { - conf.beginGroup(roomId); - QString roomName = conf.value("name").toString(); + 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")) { + if (!conf->contains("mgrIP")) { qDebug() << "Invalid config file (room " << roomName << " needs a mgrIP)!"; return; } QMap clientPositions; // First store all room configurations in _rooms. - int size = conf.beginReadArray("client"); + int size = conf->beginReadArray("client"); for (int j = 0; j < size; j++) { - conf.setArrayIndex(j); + conf->setArrayIndex(j); // qDebug() << "ip: " << conf.value("ip").toString() << " pos: " << conf.value("pos").toPoint(); // roomsList[i].insert(conf.value("ip").toString(), conf.value("pos").toPoint()); - clientPositions.insert(conf.value("ip").toString(), conf.value("pos").toPoint()); + clientPositions.insert(conf->value("ip").toString(), conf->value("pos").toPoint()); } - conf.endArray(); + 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(); + 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("gridSize")) { + gridSize = conf->value("gridSize").toSize(); } - if (conf.contains("clientSize")) { - clientSize = conf.value("clientSize").toSize(); + if (conf->contains("clientSize")) { + clientSize = conf->value("clientSize").toSize(); } foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) { @@ -452,7 +452,7 @@ void MainWindow::tryToUseRoomTemplate() myRoom = roomName; } } - conf.endGroup(); + conf->endGroup(); if (!gridSize.isValid()) { /* ok, let's choose the minimum gridSize to fit all clients */ @@ -1280,7 +1280,7 @@ void MainWindow::onClientAuthenticated(Client* client) int x, y; bool ok; - SETTINGS(sys); + QSharedPointer sys = Global::getSettings(); ok = loadPosition(sys, client->ip(), x, y); if (x >= _tilesX || y >= _tilesY) diff --git a/src/server/mainwindow/mainwindow.h b/src/server/mainwindow/mainwindow.h index 98f5df4..b3b6f13 100644 --- a/src/server/mainwindow/mainwindow.h +++ b/src/server/mainwindow/mainwindow.h @@ -76,7 +76,7 @@ private: void placeFrameInFreeSlot(ConnectionFrame* frame, QPoint preferred = QPoint(0,0)); ConnectionFrame* createFrame(); ConnectionFrame* createFrame(QString computerId, QPoint position, QPoint gridPosition); - bool loadPosition(QSettings& settings, const QString& id, int& x, int& y); + bool loadPosition(QSharedPointer settings, const QString& id, int& x, int& y); void savePosition(ConnectionFrame *cf); void startVncServerIfNecessary(int from); void tellClientCurrentSituation(Client* client); diff --git a/src/server/util/global.cpp b/src/server/util/global.cpp index bdb462f..990eee1 100644 --- a/src/server/util/global.cpp +++ b/src/server/util/global.cpp @@ -49,3 +49,18 @@ const Room* Global::getCurrentRoom() { } bool Global::manager_only = false; bool Global::_isExam = false; + +QString Global::_iniPath = ""; + +QSharedPointer Global::getSettings() { + QSharedPointer set; + if (_iniPath == "") { + /* default location (system scope) */ + set = QSharedPointer(new QSettings(QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2")); + } else { + /* use _iniPath to find ini file */ + set = QSharedPointer(new QSettings(Global::_iniPath, QSettings::IniFormat)); + } + set->setIniCodec("UTF-8"); + return set; +} diff --git a/src/server/util/global.h b/src/server/util/global.h index 08ec00a..6ab1c42 100644 --- a/src/server/util/global.h +++ b/src/server/util/global.h @@ -12,9 +12,11 @@ #include #include #include +#include #include #include #include +#include struct Room { Room(QMap cPos, QSize grid, QSize client, QString image, QString tutor) : @@ -41,6 +43,7 @@ private: static QString _currentRoom; static bool _isExam; + static QString _iniPath; public: static const QString& sessionName() { return Global::_sessionName; } @@ -55,11 +58,16 @@ public: return _rooms; } + static void setIniPath(QString s) {_iniPath = s;}; + static QString getIniPath() { return _iniPath; }; + static QSharedPointer getSettings(); + static bool isExam() { return _isExam; } static void setExam(bool b) { _isExam = b; } static void setCurrentRoom(QString room); static const QString& getCurrentRoomName() { return _currentRoom; } + /* returns a pointer to the current room or a pointer to the constant "defaultRoom". * (NEVER returns NULL or undefined) */ static const Room* getCurrentRoom(); diff --git a/src/server/util/util.h b/src/server/util/util.h index dfb70b4..e7a918b 100644 --- a/src/server/util/util.h +++ b/src/server/util/util.h @@ -8,10 +8,6 @@ #define CERTSTORAGE ".config/openslx/pvs2/" -#define SETTINGS(name) \ - QSettings name (QSettings::IniFormat, QSettings::SystemScope, "openslx/pvs2", "pvs2"); \ - name.setIniCodec("UTF-8"); - namespace Util { -- cgit v1.2.3-55-g7522