summaryrefslogtreecommitdiffhomepage
path: root/options/bundle.cpp
blob: 517005f0f661adb0cc1587b20411608353ba1c94 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Copyright (c) 2013-2016, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#include "bundle.hpp"
#include "value.hpp"
#include "globals.hpp"

#include <cstdlib>

#include <QThread>
#include <QCoreApplication>

using namespace options;
using namespace options::globals;

namespace options::detail {

bundle::bundle(const QString& group_name) :
      group_name(group_name),
      saved(group_name),
      transient(saved)
{
}

bundle::~bundle() = default;

void bundle::reload_no_notify()
{
    if (group_name.isEmpty())
        return;

    QMutexLocker l{&mtx};

    saved = group(group_name);
    transient = saved;
}

void bundle::notify()
{
    {
        QMutexLocker l(&mtx);
        connector::notify_all_values();
    }

    emit reloading();
    emit changed();
}

void bundle::reload()
{
    {
        QMutexLocker l{&mtx};
        reload_no_notify();
        connector::notify_all_values();
    }
    emit reloading();
    emit changed();
}

void bundle::set_all_to_default()
{
    connector::set_all_to_default_();
}

void bundle::store_kv(const QString& name, QVariant&& value)
{
    if (group_name.isEmpty())
        return;

    {
        options::globals::detail::mark_ini_modified();
        QMutexLocker l{&mtx};
        transient.put(name, value);
        connector::notify_values(name);
    }

    emit changed();
}

void bundle::store_kv(const QString& name, const QVariant& value)
{
    if (group_name.isEmpty())
        return;

    store_kv(name, QVariant{value});
}

QVariant bundle::get_variant(const QString& name) const
{
    QMutexLocker l{&mtx};
    return transient.get_variant(name);
}

bool bundle::contains(const QString &name) const
{
    QMutexLocker l{&mtx};
    return transient.contains(name);
}

void bundle::save()
{
    if (QThread::currentThread() != qApp->thread()) // NOLINT
        qDebug() << "group::save - current thread not ui thread";

    if (group_name.isEmpty())
        return;

    {
        QMutexLocker l{&mtx};
        saved = transient;
        saved.save();
    }

    emit saving();
}

void bundler::reload_no_notify_()
{
    QMutexLocker l(&implsgl_mtx);

    for (auto& kv : implsgl_data)
    {
        weak bundle = kv.second;
        shared bundle_ = bundle.lock();
        if (bundle_)
        {
            //qDebug() << "bundle: reverting" << kv.first << "due to profile change";
            bundle_->reload_no_notify();
        }
    }
}

void bundler::notify_()
{
    QMutexLocker l(&implsgl_mtx);

    for (auto& kv : implsgl_data)
    {
        weak bundle = kv.second;
        shared bundle_ = bundle.lock();
        if (bundle_)
        {
            //qDebug() << "bundle: reverting" << kv.first << "due to profile change";
            bundle_->notify();
        }
    }
}

void bundler::reload_()
{
    QMutexLocker l(&implsgl_mtx);
    notify_();
    reload_no_notify_();
}

void bundler::notify() { singleton().notify_(); }
void bundler::reload_no_notify() { singleton().reload_no_notify_(); }
void bundler::reload() { singleton().reload_(); }

bundler::bundler() = default;
bundler::~bundler() = default;

std::shared_ptr<bundler::v> bundler::make_bundle_(const k& key)
{
    QMutexLocker l(&implsgl_mtx);

    using iter = decltype(implsgl_data.cbegin());
    const iter it = implsgl_data.find(key);

    if (it != implsgl_data.end())
    {
        std::shared_ptr<v> ptr = it->second.lock();
        if (ptr != nullptr)
            return ptr;
        else
            qDebug() << "ERROR: nonexistent bundle" << key;
    }

    auto shr = shared(new v(key), [this, key](v* ptr) {
        QMutexLocker l(&implsgl_mtx);

        const iter it = implsgl_data.find(key);
        if (it != implsgl_data.end())
            (void)implsgl_data.erase(it);
        else
        {
            qCritical() << "ERROR: can't find self-bundle!";
            std::abort();
        }
        delete ptr;
    });
    implsgl_data[key] = weak(shr);
    return shr;
}

bundler& bundler::singleton()
{
    static bundler ret;
    return ret;
}

} // ns options::detail

namespace options {

std::shared_ptr<bundle_> make_bundle(const QString& name)
{
    if (!name.isEmpty())
        return detail::bundler::singleton().make_bundle_(name);
    else
        return std::make_shared<bundle_>(QString());
}

} // ns options