From 024212d9ebcb9bba74ceb4d318b60d9dbf3b5a42 Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Mon, 26 Sep 2016 14:33:01 +0200 Subject: clients in exam-mode no longer send a screenshot. Also some refactoring. --- src/client/clientapp/clientapp.cpp | 68 ++++++++++++++++++++++++++++++++++ src/client/clientapp/clientapp.h | 38 +++++++++++++++++++ src/client/main.cpp | 73 +++---------------------------------- src/client/net/serverconnection.cpp | 5 +++ src/client/util/util.h | 1 + 5 files changed, 117 insertions(+), 68 deletions(-) create mode 100644 src/client/clientapp/clientapp.cpp create mode 100644 src/client/clientapp/clientapp.h (limited to 'src/client') diff --git a/src/client/clientapp/clientapp.cpp b/src/client/clientapp/clientapp.cpp new file mode 100644 index 0000000..3d2dba1 --- /dev/null +++ b/src/client/clientapp/clientapp.cpp @@ -0,0 +1,68 @@ +#include "clientapp.h" + +ClientApp::ClientApp(int& argc, char** argv) : QApplication(argc, argv), _connectionMode(ConnectionMode::None) { + /* some values */ + setOrganizationName("openslx"); + setOrganizationDomain("openslx.org"); + setApplicationName("pvsclient"); + + parseParameters(); + initConfiguration(); + + + /* TODO: Move the connection handling to ClientApp */ + if (_connectionMode == ConnectionMode::Auto) { + _toolbar = new Toolbar(true); // auto connect client without session ID. + } else if (_connectionMode == ConnectionMode::Session) { + _toolbar = new Toolbar(_sessionName.toUtf8()); // connect client with given session ID. + } else { + _toolbar = new Toolbar(); // create normal client. + } + _toolbar->setVisible(!_examMode); + + /* set translator */ + /* use system locale as language to translate gui */ + QTranslator translator; + translator.load(":pvsclient"); + installTranslator(&translator); + +}; + +/* parse arguments */ +void ClientApp::parseParameters() { + for (QString a : arguments()) { + if (a == "--exam-mode") { + _examMode = true; + } else if (a == "--auto") { + _connectionMode = ConnectionMode::Auto; + } else if (a.startsWith("--session=")) { + _connectionMode = ConnectionMode::Session; + _sessionName = a.replace("--session=", ""); + } + } +} +void ClientApp::initConfiguration() { + /* configuration */ + QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/"); + do { + // Make sure settings directory exists + USER_SETTINGS(settings); + QFileInfo fi(settings.fileName()); + QDir path(fi.path()); + qDebug() << "User settings are in:" << settings.fileName(); + if (!path.exists()) + path.mkpath(path.absolutePath()); + // Now check if settings file exists. If not, copy system default (if available) + if (!fi.exists()) + { + SYSTEM_SETTINGS(sys); + qDebug() << "System settings are in:" << sys.fileName(); + QFileInfo sysfi(sys.fileName()); + if (sysfi.exists()) + { + if (!QFile::copy(sys.fileName(), settings.fileName())) + qDebug() << "Copying default settings from " << sys.fileName() << " to " << settings.fileName() << " failed."; + } + } + } while (false); +} diff --git a/src/client/clientapp/clientapp.h b/src/client/clientapp/clientapp.h new file mode 100644 index 0000000..5571cb0 --- /dev/null +++ b/src/client/clientapp/clientapp.h @@ -0,0 +1,38 @@ +#include +#include "../toolbar/toolbar.h" +#include "../util/util.h" + + +/* define a macro `clientApp` that can be used anywhere in the program and returns a reference to the current ClientApp instance */ +#if defined(clientApp) +#undef clientApp +#endif +#define clientApp (static_cast(QCoreApplication::instance())) + +/* this class is supposed to (after complete refactoring) to encapsulate all + * state of the application. At the moment, the state is distributed within + * several widgets. With this class information access will also be easier as + * it is possible to access the current ClientApp instance from anywhere with + * the clientApp macro (like qApp) macro */ +class ClientApp : public QApplication { + + Q_OBJECT + + public: + enum ConnectionMode { None, Auto, Session }; + + private: + bool _examMode; + ConnectionMode _connectionMode; /* way of automatically connection to a session on startup*/ + QString _sessionName; /* only set when _connectionMode == Session */ + Toolbar* _toolbar; + + void initConfiguration(); + void parseParameters(); + + public: + + ClientApp(int& argc, char** argv); + + bool isExamMode() const { return _examMode; }; +}; diff --git a/src/client/main.cpp b/src/client/main.cpp index 0b3bc74..8f256d7 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -1,81 +1,18 @@ #include "toolbar/toolbar.h" +#include "clientapp/clientapp.h" #include "util/util.h" int main(int argc, char** argv) { - bool option_auto = false; - bool option_session = false; - bool option_exam_mode = false; - QString sessionName; - QApplication app(argc, argv); - app.setOrganizationName("openslx"); - app.setOrganizationDomain("openslx.org"); - app.setApplicationName("pvsclient"); + ClientApp app(argc, argv); + qsrand((uint)QDateTime::currentMSecsSinceEpoch()); for (QString a : app.arguments()) { - if (a == "--exam-mode") { - option_exam_mode = true; - } else if (a == "--auto") { - option_auto = true; - } else if (a.startsWith("--session=")) { - option_session= true; - sessionName= a.replace("--session=", ""); - } else if (a == "--usage" || a == "--help") { + if (a == "--usage" || a == "--help") { qStdout() << "Usage: pvsclient [--exam-mode] [--auto|--session=xxx|\"\"]" << endl; exit(0); - } else if (!a.endsWith("pvsclient")) { - qStdout() << "ignoring unknown argument: \"" << a << "\""; - qStdout() << "see --usage for a list of options" << endl; } } - - qsrand((uint)QDateTime::currentMSecsSinceEpoch()); - - - // Set the global path of the settings - QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, "/opt/"); - - do { - // Make sure settings directory exists - USER_SETTINGS(settings); - QFileInfo fi(settings.fileName()); - QDir path(fi.path()); - qDebug() << "User settings are in:" << settings.fileName(); - if (!path.exists()) - path.mkpath(path.absolutePath()); - // Now check if settings file exists. If not, copy system default (if available) - if (!fi.exists()) - { - SYSTEM_SETTINGS(sys); - qDebug() << "System settings are in:" << sys.fileName(); - QFileInfo sysfi(sys.fileName()); - if (sysfi.exists()) - { - if (!QFile::copy(sys.fileName(), settings.fileName())) - qDebug() << "Copying default settings from " << sys.fileName() << " to " << settings.fileName() << " failed."; - } - } - } while (false); - - // use system locale as language to translate gui - QTranslator translator; - translator.load(":pvsclient"); - app.installTranslator(&translator); - - Toolbar* toolbar; - if (option_auto) { - qDebug() << "Calling Toolbar(true) (autoConnect)"; - toolbar = new Toolbar(true); // auto connect client without session ID. - } else if (option_session) { - qDebug() << "Session ID"; - toolbar = new Toolbar(sessionName.toUtf8()); // connect client with given session ID. - } else { - qDebug() << "just client mode"; - toolbar = new Toolbar(); // create normal client. - } - if (option_exam_mode) { - toolbar->setVisible(false); - } - return app.exec(); + return app.exec(); } diff --git a/src/client/net/serverconnection.cpp b/src/client/net/serverconnection.cpp index 54d95bc..3ff16bf 100644 --- a/src/client/net/serverconnection.cpp +++ b/src/client/net/serverconnection.cpp @@ -15,6 +15,7 @@ #include "../../shared/util.h" #include "../../shared/settings.h" #include "../util/platform/blankscreen.h" +#include "../clientapp/clientapp.h" #define CHALLENGE_LEN 20 @@ -180,6 +181,10 @@ void ServerConnection::handleMsg() // message THUMB - server requests screenshot as thumbnail if (id == _THUMB) { + if (clientApp->isExamMode()) { + qDebug() << "denied request for screenshot (exam mode)"; + return; + } int x = _fromServer.getFieldString(_X).toInt(); int y = _fromServer.getFieldString(_Y).toInt(); if (x < 32) diff --git a/src/client/util/util.h b/src/client/util/util.h index 9317ba7..f61684d 100644 --- a/src/client/util/util.h +++ b/src/client/util/util.h @@ -12,6 +12,7 @@ #include #include + namespace Util { //# -- cgit v1.2.3-55-g7522