summaryrefslogtreecommitdiffhomepage
path: root/options/group.hpp
blob: b0e13a6a1a0108e8810de5300ca76c8a93be2408 (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
#pragma once

#include "export.hpp"

#include "compat/util.hpp"

#include <map>
#include <memory>
#include <QString>
#include <QList>
#include <QVariant>
#include <QSettings>
#include <QMutex>

namespace options {

// snapshot of qsettings group at given time
class OTR_OPTIONS_EXPORT group final
{
    QString name;

    static QString cur_ini_pathname;
    static std::shared_ptr<QSettings> cur_ini;
    static QMutex cur_ini_mtx;
    static int ini_refcount;
    static bool ini_modifiedp;
    struct OTR_OPTIONS_EXPORT saver_ final
    {
        QSettings& s;
        QMutex& mtx;
        QMutexLocker lck;

        ~saver_();
        saver_(QSettings& s, QMutex&);
    };
    static std::shared_ptr<QSettings> cur_settings_object();

public:
    std::map<QString, QVariant> kvs;
    group(const QString& name);
    void save() const;
    void put(const QString& s, const QVariant& d);
    bool contains(const QString& s) const;
    static QString ini_directory();
    static QString ini_filename();
    static QString ini_pathname();
    static QString ini_combine(const QString& filename);
    static QStringList ini_list();

    static void mark_ini_modified();

    template<typename t>
    OTR_NEVER_INLINE
    t get(const QString& k) const
    {
        auto value = kvs.find(k);
        if (value != kvs.cend())
            return value->second.value<t>();
        return t();
    }

    template<typename F>
    OTR_NEVER_INLINE
    static auto with_settings_object(F&& fun)
    {
        saver_ saver { *cur_settings_object(), cur_ini_mtx };

        return fun(static_cast<QSettings&>(saver.s));
    }
};

}