From 8b1facbbcdecb205dcbe5864af782b584f757f2a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 31 Oct 2018 17:24:45 +0100 Subject: Read messages from external files --- src/config.cpp | 9 +++++ src/config.h | 15 ++++++++ src/deadlinetype.cpp | 59 +++++++++++++++++++++++++++++++ src/deadlinetype.h | 23 ++++++++++++ src/saverwidget.cpp | 99 ++++++++++++++-------------------------------------- src/saverwidget.h | 12 ++----- src/ui/saver.ui | 12 ++++--- 7 files changed, 142 insertions(+), 87 deletions(-) create mode 100644 src/config.cpp create mode 100644 src/config.h create mode 100644 src/deadlinetype.cpp create mode 100644 src/deadlinetype.h diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..7dfb6b1 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,9 @@ +#include "config.h" + +const QString CONFIG_DIR("/opt/openslx/xscreensaver"); +const QString CONFIG_QSS(CONFIG_DIR + "/style.qss"); + +Config::Config() +{ + +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..9e3af29 --- /dev/null +++ b/src/config.h @@ -0,0 +1,15 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include + +extern const QString CONFIG_DIR; +extern const QString CONFIG_QSS; + +class Config +{ +public: + Config(); +}; + +#endif // CONFIG_H diff --git a/src/deadlinetype.cpp b/src/deadlinetype.cpp new file mode 100644 index 0000000..e539d65 --- /dev/null +++ b/src/deadlinetype.cpp @@ -0,0 +1,59 @@ +#include "deadlinetype.h" +#include "config.h" + +#include +#include +#include + +static const QString FILE_SHUTDOWN("shutdown"); +static const QString FILE_IDLE_KILL("idle-kill"); +static const QString FILE_NO_TIMEOUT("no-timeout"); + +static const QString CONFIG_LOCKED_SUFFIX("-locked"); + +static const QString CONFIG_MESSAGES_PATH(CONFIG_DIR + "/messages.ini"); + +static QSettings MESSAGES(CONFIG_MESSAGES_PATH, QSettings::IniFormat); + +const DeadlineType *DeadlineType::Unknown = new DeadlineType(""); +const DeadlineType *DeadlineType::Shutdown = new DeadlineType(FILE_SHUTDOWN); +const DeadlineType *DeadlineType::IdleKill = new DeadlineType(FILE_IDLE_KILL); +const DeadlineType *DeadlineType::NoTimeout = new DeadlineType(FILE_NO_TIMEOUT); + +static QString loadFileToString(const QString &fileName); + +DeadlineType::DeadlineType(const QString &name) +{ + if (name.isEmpty()) + return; + MESSAGES.setIniCodec("UTF-8"); + QString nameLocked = name + CONFIG_LOCKED_SUFFIX; + _headerLine[0] = MESSAGES.value(name).toString(); + _bodyText[0] = loadFileToString(CONFIG_DIR + "/text-" + name); + if (_headerLine[0].isEmpty()) { + _headerLine[0] = CONFIG_MESSAGES_PATH + " missing key " + name; + } + if (_bodyText[0].isEmpty()) { + _bodyText[0] = CONFIG_DIR + "/text-" + name + " missing"; + } + _headerLine[1] = MESSAGES.value(nameLocked).toString(); + _bodyText[1] = loadFileToString(CONFIG_DIR + "/text-" + nameLocked); + if (_headerLine[1].isEmpty()) { + _headerLine[1] = _headerLine[0]; + } + if (_bodyText[1].isEmpty()) { + _bodyText[1] = _bodyText[0]; + } +} + +static QString loadFileToString(const QString &fileName) +{ + QFile f(fileName); + QString str; + if (f.open(QFile::ReadOnly | QFile::Text)) { + QTextStream s(&f); + s.setCodec("UTF-8"); + str = s.readAll(); + } + return str; +} diff --git a/src/deadlinetype.h b/src/deadlinetype.h new file mode 100644 index 0000000..ccf805d --- /dev/null +++ b/src/deadlinetype.h @@ -0,0 +1,23 @@ +#ifndef DEADLINETYPE_H +#define DEADLINETYPE_H + +#include + +class DeadlineType +{ + +public: + static const DeadlineType *Unknown; + static const DeadlineType *Shutdown; + static const DeadlineType *IdleKill; + static const DeadlineType *NoTimeout; + const QString& headerLine(bool isLocked) const { return _headerLine[isLocked ? 1 : 0]; } + const QString& bodyText(bool isLocked) const { return _bodyText[isLocked ? 1 : 0]; } + +private: + DeadlineType(const QString &name); + QString _headerLine[2]; + QString _bodyText[2]; +}; + +#endif // DEADLINETYPE_H diff --git a/src/saverwidget.cpp b/src/saverwidget.cpp index 027212f..17353d6 100644 --- a/src/saverwidget.cpp +++ b/src/saverwidget.cpp @@ -1,11 +1,13 @@ #include "saverwidget.h" #include "ui_saver.h" #include "screensaver.h" +#include "deadlinetype.h" #include #include #include #include +#include #include static const QChar C_ZERO('0'); @@ -29,7 +31,7 @@ SaverWidget::SaverWidget(WId parentWinId, QWidget *parent) : _logoutDeadline(0), _shutdownDeadline(0), _counter(0), - _deadlineType(DeadlineType::Unspecified), + _deadlineType(DeadlineType::Unknown), _standbyDisabled(false), _isLocked(false) { @@ -40,52 +42,37 @@ SaverWidget::SaverWidget(WId parentWinId, QWidget *parent) : if (_counter % 60 == 0) { this->reloadValues(); } - if (_counter % 10 == 0) { + if (_counter % 4 == 0) { _isLocked = ScreenSaver::isLocked(); } _counter++; qlonglong now = QDateTime::currentMSecsSinceEpoch() / 1000; // Determine what's timing out next (session/schedule) - DeadlineType type = DeadlineType::Unspecified; - qlonglong shutdownRemaining = _shutdownDeadline - now; - qlonglong logoutRemaining = _logoutDeadline - now; - if (shutdownRemaining > -330 && (shutdownRemaining < logoutRemaining || logoutRemaining <= -330)) { - type = DeadlineType::Shutdown; - } else if (logoutRemaining > -330) { - type = (_isLocked ? DeadlineType::IdleKillLocked : DeadlineType::IdleKill); - } - if (type == DeadlineType::Shutdown) { - // Shutdown is about to happen (sooner than idle logout) - if (shutdownRemaining < 0) { - ui->lblHeader->setText(QString("Dieser Rechner ist seit %1 ausgeschaltet (theoretisch zumindest)").arg(formatTime(-shutdownRemaining))); - } else { - ui->lblHeader->setText(QString("Achtung: Rechner wird in %1 heruntergefahren!").arg(formatTime(shutdownRemaining))); - } - } else if (type == DeadlineType::IdleKillLocked) { - // Idle logout is about to happen - if (logoutRemaining < 0) { - //ui->lblHeader->setText("Diese Sitzung kann durch einen Klick auf 'New Login' beendet werden..."); - ui->lblHeader->setText("Diese Sitzung wird in Kürze beendet..."); - } else { - ui->lblHeader->setText(QString("Diese Sitzung wird in %1 beendet, wenn sie nicht entsperrt wird.").arg(formatTime(logoutRemaining))); - } - } else if (type == DeadlineType::IdleKill) { - // Idle logout is about to happen, but password is not required to reactivate session - if (logoutRemaining < 0) { - ui->lblHeader->setText("Hier hat wohl jemand vergessen sich auszuloggen..."); - } else { - ui->lblHeader->setText(QString("Diese Sitzung wird in %1 beendet, wenn sie nicht weiter verwendet wird.").arg(formatTime(logoutRemaining))); - } - } else { - if (_isLocked) { - ui->lblHeader->setText("Rechner gesperrt"); + const DeadlineType *type; + qlonglong remaining = 0; + do { + qlonglong shutdownRemaining = _shutdownDeadline - now; + qlonglong logoutRemaining = _logoutDeadline - now; + if (shutdownRemaining > -330 && (shutdownRemaining < logoutRemaining || logoutRemaining <= -330)) { + type = DeadlineType::Shutdown; + remaining = shutdownRemaining; + } else if (logoutRemaining > -330) { + type = DeadlineType::IdleKill; + remaining = logoutRemaining; } else { - ui->lblHeader->setText("Schon schon schon..."); + type = DeadlineType::NoTimeout; } + } while (0); + if (remaining < 0) { + remaining = 0; } + ui->lblHeader->setText(type->headerLine(_isLocked).arg(formatTime(remaining))); ui->lblClock->setText(QDateTime::currentDateTime().toString(QLocale().dateFormat() + " HH:mm ")); - displayText(type); - bool shouldDisable = (logoutRemaining > -330 && logoutRemaining < 310) || (shutdownRemaining > -330 && shutdownRemaining < 310); + if (_deadlineType != type) { + _deadlineType = type; + ui->lblText->setText(type->bodyText(_isLocked)); + } + bool shouldDisable = (remaining > 0 && remaining < 310); if (_standbyDisabled != shouldDisable) { _standbyDisabled = shouldDisable; if (shouldDisable) { @@ -97,7 +84,6 @@ SaverWidget::SaverWidget(WId parentWinId, QWidget *parent) : } }); t->start(); - this->setMouseTracking(true); reloadValues(); } @@ -117,40 +103,7 @@ void SaverWidget::reloadValues() _logoutDeadline = s.value("lockDeadline", 0).toLongLong(); _shutdownDeadline = s.value("shutdownDeadline", 0).toLongLong(); // TODO: Load text strings for everything - _deadlineType = DeadlineType::Unspecified; -} - -void SaverWidget::displayText(DeadlineType dlt) -{ - if (_deadlineType == dlt) - return; - _deadlineType = dlt; - if (dlt == DeadlineType::IdleKillLocked) { - ui->lblText->setText("
" - "Zum oben angegebenen Zeitpunkt wird die aktuell laufende Sitzung beendet, " - "wenn sie zuvor nicht wieder entsperrt wird.
" - "Alle noch laufenden Programme (z.B. Word, Photoshop, Matlab, etc.)
" - "werden ohne Nachfrage geschlossen. Stellen Sie daher sicher, bis zum angegebenen Zeitpunkt
" - "sämtliche sich in Bearbeitung befindlichen Daten abzuspeichern, bzw. die Sitzung wieder zu entsperren.

" - "Dies dient dazu zu vermeiden, dass ein Rechner stundenlang gesperrt wird und somit
" - "anderen Nutzern nicht zur Verfügung steht."); - } else if (dlt == DeadlineType::IdleKill) { - ui->lblText->setText("Keine Nutzeraktivität festgestellt.
" - "Zum oben angegebenen Zeitpunkt wird die aktuell laufende Sitzung beendet, " - "wenn der Rechner nicht mehr verwendet wird.
" - "Alle noch laufenden Programme (z.B. Word, Photoshop, Matlab, etc.)
" - "werden ohne Nachfrage geschlossen. Stellen Sie daher sicher, bis zum angegebenen Zeitpunkt
" - "sämtliche sich in Bearbeitung befindlichen Daten abzuspeichern.

" - "Dies dient dazu zu vermeiden, dass ein Rechner stundenlang gesperrt wird und somit
" - "anderen Nutzern nicht zur Verfügung steht."); - } else if (dlt == DeadlineType::Shutdown) { - ui->lblText->setText("Achtung: Zum oben angegebenen Zeitpunkt wird der Computer heruntergefahren bzw. neugestartet.
" - "Alle noch laufenden Programme (z.B. Word, Photoshop, Matlab, etc.)
" - "werden ohne Nachfrage beendet. Stellen Sie daher sicher, bis zum angegebenen Zeitpunkt
" - "sämtliche Daten abzuspeichern und die Sitzung zu verlassen."); - } else { - ui->lblText->setText(""); - } + _deadlineType = DeadlineType::Unknown; } void SaverWidget::mouseMoveEvent(QMouseEvent *) diff --git a/src/saverwidget.h b/src/saverwidget.h index 86eee0c..bb76bbf 100644 --- a/src/saverwidget.h +++ b/src/saverwidget.h @@ -7,12 +7,7 @@ namespace Ui { class Saver; } -enum class DeadlineType { - Unspecified, - Shutdown, - IdleKill, - IdleKillLocked, -}; +class DeadlineType; class SaverWidget : public QWidget { @@ -26,8 +21,7 @@ protected: void mouseMoveEvent(QMouseEvent *e) override; protected slots: - void reloadValues(); - void displayText(DeadlineType dlt); + void reloadValues(); private: Ui::Saver *ui; @@ -37,7 +31,7 @@ private: qlonglong _logoutDeadline; qlonglong _shutdownDeadline; // Actually used for reboot, standby too... Same result for user (session = dead) int _counter; - DeadlineType _deadlineType; + const DeadlineType *_deadlineType; bool _standbyDisabled; bool _isLocked; }; diff --git a/src/ui/saver.ui b/src/ui/saver.ui index 7927e2f..8f1dc93 100644 --- a/src/ui/saver.ui +++ b/src/ui/saver.ui @@ -24,8 +24,13 @@ } QLabel { -color: #f64; -} + color: #f64; +} + +#lblClock { + color: #999; +} + @@ -78,9 +83,6 @@ color: #f64; 20 - - color: #999; - -- cgit v1.2.3-55-g7522