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

#include "export.hpp"
#include "compat/macros.hpp"

#include <optional>

#include <QString>
#include <QSettings>
#include <QMutex>

namespace options::globals::detail {

struct saver_;

struct OTR_OPTIONS_EXPORT ini_ctx
{
    std::optional<QSettings> qsettings { std::in_place };
    QString pathname;
    QMutex mtx { QMutex::Recursive };

    int refcount = 0;
    bool modifiedp = false;

    ini_ctx();
};

struct OTR_OPTIONS_EXPORT saver_ final
{
    ini_ctx& ctx;

    cc_noinline ~saver_();
    explicit cc_noinline saver_(ini_ctx& ini);
};

template<typename F>
cc_noinline
auto with_settings_object_(ini_ctx& ini, F&& fun)
{
    saver_ saver { ini };

    return fun(*ini.qsettings);
}

OTR_OPTIONS_EXPORT ini_ctx& cur_settings();
OTR_OPTIONS_EXPORT ini_ctx& global_settings();
OTR_OPTIONS_EXPORT bool is_portable_installation();

} // ns options::globals::detail

namespace options::globals
{
    OTR_OPTIONS_EXPORT void mark_ini_modified(bool value = true);
    OTR_OPTIONS_EXPORT bool is_ini_modified();
    OTR_OPTIONS_EXPORT QString ini_directory();
    OTR_OPTIONS_EXPORT QString ini_filename();
    OTR_OPTIONS_EXPORT QString ini_pathname();
    OTR_OPTIONS_EXPORT QString ini_combine(const QString& filename);
    OTR_OPTIONS_EXPORT QStringList ini_list();

    template<typename F>
    auto with_settings_object(F&& fun)
    {
        using namespace detail;
        return with_settings_object_(cur_settings(), fun);
    }

    template<typename F>
    auto with_global_settings_object(F&& fun)
    {
        using namespace detail;
        return with_settings_object_(global_settings(), fun);
    }
} // ns options::globals