summaryrefslogtreecommitdiffstats
path: root/src/deadlinetype.cpp
blob: e539d652f25c86e768097b40f79842c874346f94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "deadlinetype.h"
#include "config.h"

#include <QFile>
#include <QSettings>
#include <QTextStream>

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;
}