summaryrefslogtreecommitdiffstats
path: root/src/GreetingSetting.hpp
blob: 20fce0b5644e2b97cf5bbe1ce5de088950a9806f (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
102
103
104
105
106
107
108
109
#pragma once

#include <QString>

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

class GreetingSetting {
public:
    enum Mode {Normal, Notice, Disabled, Unset} mode = Normal;
    ClientInterface interface;
    QString target = "";

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

    inline GreetingSetting& invert()
    {
        mode = (mode == Disabled) ? Normal : Disabled;
        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;
        GreetingSetting::Mode mode = (value.at(0) == '!') ? GreetingSetting::Disabled : (value.at(0) == '-') ? GreetingSetting::Notice : GreetingSetting::Normal;
        if(mode != GreetingSetting::Normal)
        {
            val.remove(0, 1);
        }

        const QStringList& parts = Util::splitEscaped(val, ':');
        if(parts.first() == "Clonk" && parts.length() == 1)
        {
            conf.interface = Clonk;
            conf.mode = mode == GreetingSetting::Disabled ? GreetingSetting::Disabled : GreetingSetting::Normal;
            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();
            }
            conf.mode = mode;
        }
        else
        {
            throw ConfigException("Can not parse corrupt GreetingSetting");
        }
    }

    QString value()
    {
        QString ret;
        switch(conf.mode)
        {
            case GreetingSetting::Disabled:
                ret = "!";
                break;
            case GreetingSetting::Notice:
                ret = "-";
                break;
            case GreetingSetting::Normal: case GreetingSetting::Unset:
                break;
        }

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