summaryrefslogtreecommitdiffstats
path: root/backend/dconf/dconfsettings.cpp
blob: 63ecce28bdf3a9e44856f1d6bf554532aa97fee4 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is prototype code for the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "dconfsettings.h"
#include <qvariant.h>
#include <qdebug.h>
extern "C" {
#include <dconf/dconf.h>
}

DConfSettings::DConfSettings(QObject *parent) : Settings(parent)
{
    client = dconf_client_new();
    g_object_ref(client);

    g_signal_connect(G_OBJECT(client), "changed", G_CALLBACK(changeCallback), this);

    dconf_client_watch_fast(client, "/org/qt-project/"); // TODO
}

DConfSettings::~DConfSettings()
{
    dconf_client_unwatch_fast(client, "/org/qt-project/"); // TODO

    g_object_unref(client);
}

void DConfSettings::remove(const QString &key)
{
    set(key, QVariant());
}

void DConfSettings::set(const QString &key, const QVariant &value)
{
    GVariant *var = toGVariant(value);
    dconf_client_write_fast(client, key.toUtf8(), var, NULL);
}

bool DConfSettings::get(const QString &key, QVariant *value) const
{
    GVariant *var = dconf_client_read(client, key.toUtf8());
    if (var) {
        if (value)
            *value = toQVariant(var);
        g_variant_unref(var);
        return true;
    }
    return false;
}

QStringList DConfSettings::children(const QString &prefix, ChildSpec spec) const
{
    QString pfx = prefix;
    if (!pfx.endsWith(QLatin1Char('/')))
        pfx.append(QLatin1Char('/'));

    QStringList lst;
    gint len = 0;

    gchar **arr = dconf_client_list(client, pfx.toUtf8(), &len);
    for (int i = 0; i < len; ++i) {
        const QString value = QString::fromUtf8(arr[i]);
        const QByteArray path = QString(pfx + value).toUtf8();
        if (spec == AllKeys || (spec == ChildKeys && dconf_is_key(path, NULL)) || (spec == ChildGroups && dconf_is_dir(path, NULL)))
            lst += value;
    }
    g_strfreev(arr);
    return lst;
}

void DConfSettings::clear()
{
}

void DConfSettings::sync()
{
    dconf_client_sync(client);
}

void DConfSettings::flush()
{
    sync();
}

bool DConfSettings::isWritable() const
{
    return dconf_client_is_writable(client, NULL);
}

QString DConfSettings::fileName() const
{
    return QString();
}

QVariant DConfSettings::toQVariant(GVariant *value)
{
    if (!value)
        return QVariant();

    switch (g_variant_classify(value)) {
    case G_VARIANT_CLASS_BOOLEAN:
        return QVariant::fromValue(g_variant_get_boolean(value));
    case G_VARIANT_CLASS_BYTE:
        return QVariant::fromValue(g_variant_get_byte(value));
    case G_VARIANT_CLASS_INT16:
        return QVariant::fromValue(g_variant_get_int16(value));
    case G_VARIANT_CLASS_UINT16:
        return QVariant::fromValue(g_variant_get_uint16(value));
    case G_VARIANT_CLASS_INT32:
        return QVariant::fromValue(g_variant_get_int32(value));
    case G_VARIANT_CLASS_UINT32:
        return QVariant::fromValue(g_variant_get_uint32(value));
    case G_VARIANT_CLASS_INT64:
        return QVariant::fromValue(g_variant_get_int64(value));
    case G_VARIANT_CLASS_UINT64:
        return QVariant::fromValue(g_variant_get_uint64(value));
    case G_VARIANT_CLASS_DOUBLE:
        return QVariant::fromValue(g_variant_get_double(value));
    case G_VARIANT_CLASS_STRING:
    case G_VARIANT_CLASS_OBJECT_PATH:
    case G_VARIANT_CLASS_SIGNATURE:
        return QString::fromUtf8(g_variant_get_string(value, NULL));
    // TODO:
    case G_VARIANT_CLASS_HANDLE:
    case G_VARIANT_CLASS_VARIANT:
    case G_VARIANT_CLASS_MAYBE:
    case G_VARIANT_CLASS_ARRAY:
    case G_VARIANT_CLASS_TUPLE:
    case G_VARIANT_CLASS_DICT_ENTRY:
    default:
        return QVariant();
    }
}

GVariant* DConfSettings::toGVariant(const QVariant& value)
{
    if (!value.isValid())
        return NULL;

    switch (value.type()) {
    case QVariant::Invalid:
        return NULL;
    case QVariant::Bool:
        return g_variant_new_boolean(value.toBool());
    case QVariant::Int:
        return g_variant_new_int32(value.toInt());
    case QVariant::UInt:
        return g_variant_new_uint32(value.toUInt());
    case QVariant::LongLong:
        return g_variant_new_int64(value.toLongLong());
    case QVariant::ULongLong:
        return g_variant_new_uint64(value.toULongLong());
    case QVariant::Double:
        return g_variant_new_double(value.toDouble());
    case QVariant::Char:
        return g_variant_new_byte(value.toChar().toLatin1());
    case QVariant::String:
        return g_variant_new_string(value.toString().toUtf8());
    case QVariant::ByteArray:
        return g_variant_new_bytestring(value.toByteArray());
    case QVariant::Map:
    case QVariant::List:
    case QVariant::StringList:
    case QVariant::BitArray:
    case QVariant::Date:
    case QVariant::Time:
    case QVariant::DateTime:
    case QVariant::Url:
    case QVariant::Locale:
    case QVariant::Rect:
    case QVariant::RectF:
    case QVariant::Size:
    case QVariant::SizeF:
    case QVariant::Line:
    case QVariant::LineF:
    case QVariant::Point:
    case QVariant::PointF:
    case QVariant::RegExp:
    case QVariant::RegularExpression:
    case QVariant::Hash:
    case QVariant::EasingCurve:
    case QVariant::Uuid:
    case QVariant::ModelIndex:

    case QVariant::Font:
    case QVariant::Pixmap:
    case QVariant::Brush:
    case QVariant::Color:
    case QVariant::Palette:
    case QVariant::Image:
    case QVariant::Polygon:
    case QVariant::Region:
    case QVariant::Bitmap:
    case QVariant::Cursor:
    case QVariant::KeySequence:
    case QVariant::Pen:
    case QVariant::TextLength:
    case QVariant::TextFormat:
    case QVariant::Matrix:
    case QVariant::Transform:
    case QVariant::Matrix4x4:
    case QVariant::Vector2D:
    case QVariant::Vector3D:
    case QVariant::Vector4D:
    case QVariant::Quaternion:
    case QVariant::PolygonF:
    case QVariant::Icon:

    case QVariant::SizePolicy:

    default:
        return NULL;
    }
}

void DConfSettings::changeCallback(DConfClient *client, char *prefix, char **changes, char *tag, void *userData)
{
    Q_UNUSED(client);
    Q_UNUSED(tag);

    DConfSettings *settings = static_cast<DConfSettings*>(userData);
    while (settings && changes && changes[0]) {
        const QString key = QString::fromUtf8(changes[0]);
        QVariant value;
        settings->get(prefix, &value);
        qDebug() << Q_FUNC_INFO << prefix << key << value;
        Q_EMIT settings->changed(key, value);
        ++changes;
    }
}