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/deadlinetype.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/deadlinetype.cpp (limited to 'src/deadlinetype.cpp') 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; +} -- cgit v1.2.3-55-g7522