aboutsummaryrefslogtreecommitdiff
path: root/lib/settings/configuration.h
blob: 82e0c58de3cfaa79eada02c704da1e3a424ca373 (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
110
111
112
113
114
115
116
117
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <libconfig.h++>
#include <optional>
#include <string>
#include <vector>
#include <QString>

class Configuration
{
public:
    explicit Configuration(const std::string &path, const std::string &home);
    ~Configuration();

    bool read(const QString &path);
    bool writeIfNeeded(const std::string &path = std::string());

    std::vector<std::string> childrenSettings(const char *name = "");
    std::vector<std::string> childrenGroups(const char *name = "");

    template <typename T>
    std::optional<T> value(const char *path) const
    {
        // if setting doesn't exist, give back a nullopt
        if(!m_userCfg->exists(path)) {
            return std::nullopt;
        }

        const libconfig::Setting &v = m_userCfg->lookup(path);
        if constexpr(std::is_same_v<T, std::string>) {
            std::string r = castToString(v);

            // check if it's a path
            if(r.front() == '~') {
                r.replace(0, 1, m_homePath);
            }

            return std::optional<std::string>(r);
        } else
            return std::optional<T>(static_cast<T>(v));
    }

    template <typename T>
    bool setValue(std::string path, const T &val)
    {
        if(!m_userCfg->exists(path)) {
            return false;
        }

        libconfig::Setting &setting = m_userCfg->lookup(path);
        // compiler complained about operator= not taking unsinged ints, longs and long longs
        if constexpr(std::is_unsigned_v<T> && !std::is_same_v<T, bool>) {
            setting = static_cast<typename std::make_signed_t<T>>(val);
        } else if constexpr(std::is_same_v<T, std::string>) {
            switch(setting.getType()) {
            case libconfig::Setting::TypeNone:
                break;

            case libconfig::Setting::TypeInt:
            case libconfig::Setting::TypeInt64:
                setting = std::stoi(static_cast<std::string>(val));
                break;

            case libconfig::Setting::TypeFloat:
                setting = std::stod(static_cast<std::string>(val));
                break;

            case libconfig::Setting::TypeString:
                setting = static_cast<std::string>(val).c_str();
                break;

            case libconfig::Setting::TypeBoolean:
                if(static_cast<std::string>(val) == "true") {
                    setting = true;
                } else if(static_cast<std::string>(val) == "false") {
                    setting = false;
                }
                break;

            case libconfig::Setting::TypeGroup:
                break;
            case libconfig::Setting::TypeArray:
                break;
            case libconfig::Setting::TypeList:
                break;
            }

        } else {
            setting = val;
        }

        changed = true;
        return true;
    }

private:
    std::string castToString(const libconfig::Setting &v) const;

    bool changed = false;
    std::string m_homePath;
    std::string m_userCfgPath;
    libconfig::Config *m_userCfg;
};

// replace ~ with home
std::string patchHome(const std::string &path, const std::string &home);

#endif // CONFIGURATION_H