summaryrefslogtreecommitdiffhomepage
path: root/options/globals.cpp
blob: 39eb6014f12c1ee360f5833108f4fefa15499a48 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "globals.hpp"
#include "compat/base-path.hpp"
#include "defs.hpp"
#include "opentrack-org.hxx"

#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QStandardPaths>
#include <QDateTime>
#include <QDebug>

namespace options::globals::detail {

ini_ctx::ini_ctx() = default;

static bool is_portable_installation()
{
#if defined _WIN32
    // must keep consistent between invocations
    static const bool ret = QFile::exists(OPENTRACK_BASE_PATH + "/portable.txt");
    return ret;
#else
    return false;
#endif
}

saver_::~saver_()
{
    if (--ctx.refcount == 0 && ctx.modifiedp)
    {
        auto& settings = *ctx.qsettings;
        settings.sync();
        if (settings.status() != QSettings::NoError)
            qDebug() << "error with .ini file" << settings.fileName() << settings.status();
        ctx.modifiedp = false;
    }
    ctx.mtx.unlock();
}

saver_::saver_(ini_ctx& ini) : ctx { ini }
{
    ctx.refcount++;
}

ini_ctx& cur_settings()
{
    static ini_ctx ini;
    const QString filename = ini_filename();

    ini.mtx.lock();

    if (ini.pathname != filename)
    {
        ini.qsettings.emplace(ini_combine(filename), QSettings::IniFormat);
        ini.pathname = filename;
    }

    return ini;
}

ini_ctx& global_settings()
{
    static ini_ctx& ret = progn(
        static ini_ctx ini;

        if (!is_portable_installation())
            // Windows registry or xdg on Linux
            ini.qsettings.emplace(OPENTRACK_ORG);
        else
            // file in executable's directory
            ini.qsettings.emplace(OPENTRACK_BASE_PATH + QStringLiteral("/globals.ini"),
                                  QSettings::IniFormat);

        ini.pathname = QStringLiteral(".");
        return (ini_ctx&)ini;
    );
    ret.mtx.lock();
    return ret;
}

void mark_ini_modified(bool value)
{
    auto& ini = cur_settings();
    ini.modifiedp = value;
    ini.mtx.unlock();
}

} // ns options::globals::detail

namespace options::globals
{

using namespace detail;

bool is_ini_modified()
{
    ini_ctx& ini = cur_settings();
    bool ret = ini.modifiedp;
    ini.mtx.unlock();
    return ret;
}

QString ini_filename()
{
    return with_global_settings_object([&](QSettings& settings) {
        static_assert(sizeof(OPENTRACK_DEFAULT_PROFILE) > 1);
        static_assert(sizeof(OPENTRACK_PROFILE_FILENAME_KEY) > 1);

        const QString ret = settings.value(QStringLiteral(OPENTRACK_PROFILE_FILENAME_KEY),
                                           QStringLiteral(OPENTRACK_DEFAULT_PROFILE)).toString();
        if (ret.isEmpty())
            return QStringLiteral(OPENTRACK_DEFAULT_PROFILE);
        return ret;
    });
}

QString ini_pathname()
{
    return ini_combine(ini_filename());
}

QString ini_combine(const QString& filename)
{
    return QStringLiteral("%1/%2").arg(ini_directory(), filename);
}

QStringList ini_list()
{
    static QMutex mtx;
    static QStringList list;
    QMutexLocker l{&mtx};

    const QString dirname = ini_directory();

    {
        static QDateTime last_time = {};
        auto time = QFileInfo{dirname}.lastModified();
        if (time == last_time)
            return list;
        last_time = time;
    }

    QDir settings_dir(dirname);

    using f = QDir::Filter;
    list = settings_dir.entryList({ QStringLiteral("*.ini") }, f::Files | f::Readable, QDir::Name);
    std::sort(list.begin(), list.end());
    return list;
}

void mark_global_ini_modified(bool value)
{
    auto& ini = global_settings();
    ini.modifiedp = value;
    ini.mtx.unlock();
}

static QString ini_directory_()
{
    if (detail::is_portable_installation())
    {
fail:   constexpr const char* subdir = "ini";
        QString dir = OPENTRACK_BASE_PATH;
        if (dir.isEmpty())
            dir = '.';
        (void)QDir(dir).mkpath(subdir);
        return QStringLiteral("%1/%2").arg(dir, subdir);
    }
    else
    {
        QString dir = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).value(0, QString());
        if (dir.isEmpty())
            goto fail;
        const QString fmt = QStringLiteral("%1/%2");
#if !defined _WIN32 && !defined __APPLE__
        if (!QFile::exists(fmt.arg(dir, OPENTRACK_ORG)))
        {
            dir = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).value(0, QString());
            if (dir.isEmpty())
                goto fail;
        }
#endif
        (void)QDir(dir).mkpath(OPENTRACK_ORG);
        return fmt.arg(dir, OPENTRACK_ORG);
    }
}

QString ini_directory()
{
    static const QString dir = ini_directory_();
    return dir;
}

} // ns options::globals