summaryrefslogtreecommitdiffstats
path: root/src/GreetingSetting.hpp
blob: 933d4c57e11bed14644f296642cea9da75521ff3 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once

#include <QString>

#include "qt-config/ConfigBase.hpp"
#include "ClientInterface.hpp"

class GreetingSetting {
public:
    ClientInterface interface;
    bool negative = false;
    QString target = "";

    GreetingSetting() { }
    inline bool operator==(const GreetingSetting& other)
    {
        return other.interface == interface && other.negative == negative && other.target == target;
    }
    inline bool operator!=(const GreetingSetting& other)
    {
        return !operator==(other);
    }

    inline GreetingSetting& invert()
    {
        negative = !negative;
        return *this;
    }

    static GreetingSetting clonk()
    {
        GreetingSetting ret;
        ret.interface = Clonk;
        return ret;
    }

    static GreetingSetting irc(QString target = "")
    {
        GreetingSetting ret;
        ret.interface = IRC;
        ret.target = target;
        return ret;
    }
};

template <>
class ConfigValue<GreetingSetting> : ConfigValueBase {
    GreetingSetting& conf;
public:
    ConfigValue(GreetingSetting& conf) : conf(conf) {  }

    void setValue(const QString& value)
    {
        QString val = value;
        bool negative = (value.at(0) == '!');
        if(negative)
        {
            val.remove(0, 1);
        }

        const QStringList& parts = Util::splitEscaped(val, ':');
        if(parts.first() == "Clonk" && parts.length() == 1)
        {
            conf.interface = Clonk;
            conf.target.clear();
        }
        else if(parts.first() == "IRC" && parts.length() <= 2)
        {
            conf.interface = IRC;
            if(parts.length() == 2)
            {
                conf.target = parts.at(1);
            }
            else
            {
                conf.target.clear();
            }
        }
        else
        {
            throw ConfigException("Can not parse corrupt GreetingSetting");
        }

        conf.negative = negative;
    }

    QString value()
    {
        QString ret;
        if(conf.negative)
        {
            ret = "!";
        }
        QStringList parts = {(conf.interface == Clonk ? "Clonk" : "IRC")};
        if(!conf.target.isEmpty())
        {
            parts.append(conf.target);
        }
        return ret + Util::joinEscape(parts, ':');
    }
};