ruleset.cpp 1.62 KB
Newer Older
Parker Coates's avatar
Parker Coates committed
1
/*
2
3
4
5
6
7
    This file is part of Killbots.

    SPDX-FileCopyrightText: 2007-2009 Parker Coates <coates@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
Parker Coates's avatar
Parker Coates committed
8
9
10

#include "ruleset.h"

Laurent Montel's avatar
Laurent Montel committed
11
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
12
#include "killbots_debug.h"
Laurent Montel's avatar
Laurent Montel committed
13

14
#include <QFileInfo>
Parker Coates's avatar
Parker Coates committed
15

Laurent Montel's avatar
Laurent Montel committed
16
const Killbots::Ruleset *Killbots::Ruleset::load(const QString &fileName)
Parker Coates's avatar
Parker Coates committed
17
{
18
    const Ruleset *result = nullptr;
Laurent Montel's avatar
Laurent Montel committed
19
    if (!fileName.isEmpty()) {
20
        QString filePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("killbots/rulesets/") + fileName);
Laurent Montel's avatar
Laurent Montel committed
21
22
23
24
25
26
27
28
29
30
        if (!filePath.isEmpty()) {
            // Our only check for validity is that we can open the file as a config
            // file and that it contains a group named "KillbotsRuleset".
            KConfig configFile(filePath, KConfig::SimpleConfig);
            if (configFile.hasGroup("KillbotsRuleset")) {
                result = new Ruleset(filePath);
            }
        }
    }
    if (!result) {
Laurent Montel's avatar
Laurent Montel committed
31
        qCDebug(KILLBOTS_LOG) << "Failed to load " << fileName;
Laurent Montel's avatar
Laurent Montel committed
32
33
34
    }

    return result;
Parker Coates's avatar
Parker Coates committed
35
36
}

Laurent Montel's avatar
Laurent Montel committed
37
Killbots::Ruleset::Ruleset(const QString &filePath)
38
    : RulesetBase(KSharedConfig::openConfig(filePath))
Parker Coates's avatar
Parker Coates committed
39
{
Laurent Montel's avatar
Laurent Montel committed
40
41
    m_filePath = filePath;
    QString untranslatedName = KConfigGroup(config(), "KillbotsRuleset").readEntryUntranslated("Name");
Frederik Schwarzer's avatar
Frederik Schwarzer committed
42
    m_scoreGroupKey = untranslatedName.simplified().remove(QLatin1Char(' ')).toLatin1();
Parker Coates's avatar
Parker Coates committed
43
44
}

45
Killbots::Ruleset::~Ruleset()
Parker Coates's avatar
Parker Coates committed
46
47
48
{
}

49
QString Killbots::Ruleset::filePath() const
Parker Coates's avatar
Parker Coates committed
50
{
Laurent Montel's avatar
Laurent Montel committed
51
    return m_filePath;
Parker Coates's avatar
Parker Coates committed
52
53
}

54
QString Killbots::Ruleset::fileName() const
Parker Coates's avatar
Parker Coates committed
55
{
Laurent Montel's avatar
Laurent Montel committed
56
    return QFileInfo(m_filePath).fileName();
Parker Coates's avatar
Parker Coates committed
57
58
}

59
QByteArray Killbots::Ruleset::scoreGroupKey() const
Parker Coates's avatar
Parker Coates committed
60
{
Laurent Montel's avatar
Laurent Montel committed
61
    return m_scoreGroupKey;
62
}