diff options
author | J-P Nurmi <jpnurmi@digia.com> | 2013-07-09 15:59:18 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@digia.com> | 2013-07-09 16:05:21 +0200 |
commit | 3cedaefda1bc407c716979fcd7db3edcf42704c3 (patch) | |
tree | aea783b292f549dca93ad819d02f7574cabe90dd /backend | |
parent | 31fd27d770faf57d2f07814580a6630fc0e708ef (diff) |
Import DConf-based QSettings backend prototype
Change-Id: Ic18f82e1da5fb8de33d0eaae51b40e97bf1ab8a4
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Diffstat (limited to 'backend')
-rw-r--r-- | backend/backend.pri | 11 | ||||
-rw-r--r-- | backend/dconf/dconfsettings.cpp | 267 | ||||
-rw-r--r-- | backend/dconf/dconfsettings.h | 79 |
3 files changed, 357 insertions, 0 deletions
diff --git a/backend/backend.pri b/backend/backend.pri new file mode 100644 index 0000000..70f84d1 --- /dev/null +++ b/backend/backend.pri @@ -0,0 +1,11 @@ +INCLUDEPATH += $$PWD/dconf +DEPENDPATH += $$PWD/dconf + +CONFIG += no_keywords link_pkgconfig +PKGCONFIG += dconf + +SOURCES += \ + $$PWD/dconf/dconfsettings.cpp + +HEADERS += \ + $$PWD/dconf/dconfsettings.h diff --git a/backend/dconf/dconfsettings.cpp b/backend/dconf/dconfsettings.cpp new file mode 100644 index 0000000..63ecce2 --- /dev/null +++ b/backend/dconf/dconfsettings.cpp @@ -0,0 +1,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; + } +} diff --git a/backend/dconf/dconfsettings.h b/backend/dconf/dconfsettings.h new file mode 100644 index 0000000..1cdfa72 --- /dev/null +++ b/backend/dconf/dconfsettings.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ +#ifndef DCONFSETTINGS_H +#define DCONFSETTINGS_H + +#include "settings.h" + +typedef struct _GVariant GVariant; +typedef struct _DConfClient DConfClient; + +class DConfSettings : public Settings +{ + Q_OBJECT + +public: + explicit DConfSettings(QObject *parent = 0); + ~DConfSettings(); + + void remove(const QString &key); + void set(const QString &key, const QVariant &value); + bool get(const QString &key, QVariant *value) const; + + QStringList children(const QString &prefix, ChildSpec spec) const; + + void clear(); + void sync(); + void flush(); + bool isWritable() const; + QString fileName() const; + +protected: + static QVariant toQVariant(GVariant *value); + static GVariant *toGVariant(const QVariant& value); + + static void changeCallback(DConfClient *client, char *prefix, char **changes, char *tag, void *userData); + +private: + DConfClient *client; +}; + +#endif // DCONFSETTINGS_H |