gsettings-qt-0.1+14.04.20140408/0000755000015301777760000000000012320776727016420 5ustar pbusernogroup00000000000000gsettings-qt-0.1+14.04.20140408/examples/0000755000015301777760000000000012320776727020236 5ustar pbusernogroup00000000000000gsettings-qt-0.1+14.04.20140408/examples/choices.qml0000644000015301777760000000050012320776503022351 0ustar pbusernogroup00000000000000 import QtQuick 2.0 import GSettings 1.0 Item { width: 200 height: 300 GSettings { id: settings schema.id: "org.gnome.desktop.interface" } ListView { anchors.fill: parent model: settings.schema.choices('toolbarStyle') delegate { Text { text: modelData } } } } gsettings-qt-0.1+14.04.20140408/GSettings/0000755000015301777760000000000012320776727020327 5ustar pbusernogroup00000000000000gsettings-qt-0.1+14.04.20140408/GSettings/plugin.h0000644000015301777760000000160712320776503021772 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #include class GSettingsQmlPlugin: public QQmlExtensionPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "com.canonical.GSettings") public: void registerTypes(const char *uri); }; gsettings-qt-0.1+14.04.20140408/GSettings/gsettings-qml.cpp0000644000015301777760000000767512320776503023640 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #include "gsettings-qml.h" #include struct GSettingsSchemaQmlPrivate { QByteArray id; QByteArray path; bool isValid; GSettingsSchemaQmlPrivate() : isValid(false) {} }; struct GSettingsQmlPrivate { GSettingsSchemaQml *schema; QGSettings *settings; }; GSettingsSchemaQml::GSettingsSchemaQml(QObject *parent): QObject(parent) { priv = new GSettingsSchemaQmlPrivate; } GSettingsSchemaQml::~GSettingsSchemaQml() { delete priv; } QByteArray GSettingsSchemaQml::id() const { return priv->id; } void GSettingsSchemaQml::setId(const QByteArray &id) { if (!priv->id.isEmpty()) { qWarning("GSettings.schema.id may only be set on construction"); return; } priv->id = id; } QByteArray GSettingsSchemaQml::path() const { return priv->path; } void GSettingsSchemaQml::setPath(const QByteArray &path) { if (!priv->path.isEmpty()) { qWarning("GSettings.schema.path may only be set on construction"); return; } priv->path = path; } bool GSettingsSchemaQml::isValid() const { return priv->isValid; } void GSettingsSchemaQml::setIsValid(bool valid) { if (valid != priv->isValid) { priv->isValid = valid; Q_EMIT isValidChanged(); } } QVariantList GSettingsSchemaQml::choices(const QByteArray &key) const { GSettingsQml *parent = (GSettingsQml *) this->parent(); if (parent->priv->settings == NULL) return QVariantList(); if (!parent->contains(key)) return QVariantList(); return parent->priv->settings->choices(key); } void GSettingsSchemaQml::reset(const QByteArray &key) { GSettingsQml *parent = (GSettingsQml *) this->parent(); if (parent->priv->settings != NULL) parent->priv->settings->reset(key); } GSettingsQml::GSettingsQml(QObject *parent): QQmlPropertyMap(this, parent) { priv = new GSettingsQmlPrivate; priv->schema = new GSettingsSchemaQml(this); priv->settings = NULL; } GSettingsQml::~GSettingsQml() { delete priv; } GSettingsSchemaQml * GSettingsQml::schema() const { return priv->schema; } void GSettingsQml::classBegin() { } void GSettingsQml::componentComplete() { bool schemaValid = QGSettings::isSchemaInstalled(priv->schema->id()); if (schemaValid) { priv->settings = new QGSettings(priv->schema->id(), priv->schema->path(), this); connect(priv->settings, SIGNAL(changed(const QString &)), this, SLOT(settingChanged(const QString &))); Q_FOREACH(QString key, priv->settings->keys()) this->insert(key, priv->settings->get(key)); Q_EMIT(schemaChanged()); } // emit isValid notification only once everything is setup priv->schema->setIsValid(schemaValid); } void GSettingsQml::settingChanged(const QString &key) { QVariant value = priv->settings->get(key); this->insert(key, value); Q_EMIT(changed(key, value)); } QVariant GSettingsQml::updateValue(const QString& key, const QVariant &value) { if (priv->settings == NULL) return QVariant(); if (priv->settings->trySet(key, value)) { return value; } else { qWarning("unable to set key '%s' to value '%s'", key.toUtf8().constData(), value.toString().toUtf8().constData()); return priv->settings->get(key); } } gsettings-qt-0.1+14.04.20140408/GSettings/gsettings-qml.h0000644000015301777760000000430412320776503023267 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #include #include class GSettingsSchemaQml: public QObject { Q_OBJECT Q_PROPERTY(QByteArray id READ id WRITE setId) Q_PROPERTY(QByteArray path READ path WRITE setPath) Q_PROPERTY(bool isValid READ isValid NOTIFY isValidChanged) public: GSettingsSchemaQml(QObject *parent = NULL); ~GSettingsSchemaQml(); QByteArray id() const; void setId(const QByteArray &id); QByteArray path() const; void setPath(const QByteArray &path); bool isValid() const; void setIsValid(bool valid); Q_INVOKABLE QVariantList choices(const QByteArray &key) const; Q_INVOKABLE void reset(const QByteArray &key); Q_SIGNALS: void isValidChanged(); private: struct GSettingsSchemaQmlPrivate *priv; }; class GSettingsQml: public QQmlPropertyMap, public QQmlParserStatus { Q_OBJECT Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(GSettingsSchemaQml* schema READ schema NOTIFY schemaChanged) public: GSettingsQml(QObject *parent = NULL); ~GSettingsQml(); GSettingsSchemaQml * schema() const; void updateKey(const char *gkey, bool emitChanged); void classBegin(); void componentComplete(); Q_SIGNALS: void schemaChanged(); void changed (const QString &key, const QVariant &value); private Q_SLOTS: void settingChanged(const QString &key); private: struct GSettingsQmlPrivate *priv; QVariant updateValue(const QString& key, const QVariant &value); friend class GSettingsSchemaQml; }; gsettings-qt-0.1+14.04.20140408/GSettings/gsettings-qt.pro0000644000015301777760000000141112320776503023467 0ustar pbusernogroup00000000000000TEMPLATE = lib QT += qml QT -= gui CONFIG += qt plugin no_keywords link_pkgconfig PKGCONFIG += gio-2.0 INCLUDEPATH += ../src . LIBS += -L../src -lgsettings-qt TARGET = GSettingsQmlPlugin HEADERS = plugin.h gsettings-qml.h SOURCES = plugin.cpp gsettings-qml.cpp uri = GSettings API_VER = 1.0 # deployment rules for the plugin installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /).$$API_VER target.path = $$installPath INSTALLS += target extra.path = $$installPath extra.files += qmldir INSTALLS += extra qmltypes.path = $$installPath qmltypes.files = plugins.qmltypes qmltypes.extra = export LD_PRELOAD=../src/libgsettings-qt.so.1; $$[QT_INSTALL_BINS]/qmlplugindump -notrelocatable GSettings 1.0 .. > $(INSTALL_ROOT)/$$installPath/plugins.qmltypes INSTALLS += qmltypes gsettings-qt-0.1+14.04.20140408/GSettings/plugin.cpp0000644000015301777760000000207012320776503022320 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #include "plugin.h" #include "gsettings-qml.h" void GSettingsQmlPlugin::registerTypes(const char *uri) { qmlRegisterType(uri, 1, 0, "GSettings"); qmlRegisterUncreatableType(uri, 1, 0, "GSettingsSchema", "GSettingsSchema can only be used inside of a GSettings component"); } gsettings-qt-0.1+14.04.20140408/GSettings/qmldir0000644000015301777760000000010512320776503021526 0ustar pbusernogroup00000000000000module GSettings plugin GSettingsQmlPlugin typeinfo plugins.qmltypes gsettings-qt-0.1+14.04.20140408/tests/0000755000015301777760000000000012320776727017562 5ustar pbusernogroup00000000000000gsettings-qt-0.1+14.04.20140408/tests/test.cpp0000644000015301777760000000007712320776503021241 0ustar pbusernogroup00000000000000#include QUICK_TEST_MAIN(GSettings) gsettings-qt-0.1+14.04.20140408/tests/tests.pro0000644000015301777760000000104312320776503021434 0ustar pbusernogroup00000000000000TEMPLATE = app QT += qml testlib QT -= gui CONFIG += qmltestcase TARGET = test IMPORTPATH = $$PWD/.. SOURCES = test.cpp schema.target = gschemas.compiled schema.commands = glib-compile-schemas $$PWD schema.depends = com.canonical.gsettings.test.gschema.xml QMAKE_EXTRA_TARGETS += schema PRE_TARGETDEPS = gschemas.compiled # qmake prepends this to the command line executed by `make check` check.commands = QT_QPA_PLATFORM=minimal LD_LIBRARY_PATH=$$PWD/../src GSETTINGS_BACKEND=memory GSETTINGS_SCHEMA_DIR=$$PWD OTHER_FILES += tst_GSettings.qml gsettings-qt-0.1+14.04.20140408/tests/com.canonical.gsettings.test.gschema.xml0000644000015301777760000000142312320776503027372 0ustar pbusernogroup00000000000000 42 1.5 false "hello" 'one' ['one', 'two', 'three'] gsettings-qt-0.1+14.04.20140408/tests/tst_GSettings.qml0000644000015301777760000000475012320776503023074 0ustar pbusernogroup00000000000000 import QtTest 1.0 import "../GSettings" 1.0 import QtQuick 2.0 TestCase { id: testCase property var changes: [] GSettings { id: settings schema.id: "com.canonical.gsettings.Test" onChanged: changes.push([key, value]); } GSettings { id: invalid_settings schema.id: "com.canonical.gsettings.NonExisting" } property string bindingTest: settings.testString // this test must run first (others overwrite keys), hence the 'aaa' function test_aaa_read_defaults() { compare(settings.schema.isValid, true); compare(settings.testInteger, 42); compare(settings.testDouble, 1.5); compare(settings.testBoolean, false); compare(settings.testString, 'hello'); compare(settings.testStringList, ['one', 'two', 'three']); compare(testCase.bindingTest, 'hello'); } function test_write() { settings.testInteger = 2; compare(settings.testInteger, 2); settings.testDouble = 2.5; compare(settings.testDouble, 2.5); settings.testBoolean = true; compare(settings.testBoolean, true); settings.testString = 'bye'; compare(settings.testString, 'bye'); compare(testCase.bindingTest, 'bye'); settings.testStringList = ['four', 'five'] compare(settings.testStringList, ['four', 'five']); settings.testStringList = ['six'] compare(settings.testStringList, ['six']); settings.testStringList = []; compare(settings.testStringList, []); settings.testEnum = 'two'; compare(settings.testEnum, 'two'); // test whether writing an out-of-range key doesn't work settings.testEnum = 'notanumber'; compare(settings.testEnum, 'two'); } function test_changed() { changes = [] settings.testInteger = 4; settings.testDouble = 3.14 settings.testString = 'goodbye'; compare(changes, [['testInteger', 4], ['testDouble', 3.14], ['testString', 'goodbye']]); } function test_choices() { compare(settings.schema.choices('testEnum'), ['one', 'two', 'three']); compare(settings.schema.choices('testInteger'), []); } function test_non_existing() { compare(settings.schema.aKeyThatsNotInTheSchema, undefined); compare(settings.schema.choices('aKeyThatsNotInTheSchema'), []); } function test_reset() { settings.testInteger = 4; settings.schema.reset('testInteger'); compare(settings.testInteger, 42); } function test_invalid_schema() { compare(invalid_settings.schema.isValid, false); compare(invalid_settings.schema.testInteger, undefined); } } gsettings-qt-0.1+14.04.20140408/gsettings-qt.pro0000644000015301777760000000013612320776503021563 0ustar pbusernogroup00000000000000TEMPLATE = subdirs SUBDIRS += src/gsettings-qt.pro GSettings/gsettings-qt.pro tests/tests.pro gsettings-qt-0.1+14.04.20140408/COPYING0000644000015301777760000001674312320776503017456 0ustar pbusernogroup00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. gsettings-qt-0.1+14.04.20140408/src/0000755000015301777760000000000012320776727017207 5ustar pbusernogroup00000000000000gsettings-qt-0.1+14.04.20140408/src/QGSettings0000644000015301777760000000003012320776503021143 0ustar pbusernogroup00000000000000#include "qgsettings.h" gsettings-qt-0.1+14.04.20140408/src/gsettings-qt.pro0000644000015301777760000000127412320776503022356 0ustar pbusernogroup00000000000000TEMPLATE = lib INCLUDEPATH += . QT -= gui CONFIG += qt no_keywords link_pkgconfig create_pc create_prl no_install_prl PKGCONFIG += gio-2.0 TARGET = gsettings-qt HEADERS = qgsettings.h util.h qconftypes.h SOURCES = qgsettings.cpp util.cpp qconftypes.cpp target.path = $$[QT_INSTALL_LIBS] INSTALLS += target headers.path = $$[QT_INSTALL_HEADERS]/QGSettings headers.files = qgsettings.h QGSettings INSTALLS += headers QMAKE_PKGCONFIG_NAME = GSettingsQt QMAKE_PKGCONFIG_DESCRIPTION = Qt bindings to GSettings QMAKE_PKGCONFIG_PREFIX = $$INSTALLBASE QMAKE_PKGCONFIG_LIBDIR = $$target.path QMAKE_PKGCONFIG_INCDIR = $$headers.path QMAKE_PKGCONFIG_VERSION = $$VERSION QMAKE_PKGCONFIG_DESTDIR = pkgconfig gsettings-qt-0.1+14.04.20140408/src/util.cpp0000644000015301777760000000366412320776503020671 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Authors: Lars Uebernickel * Ryan Lortie */ #include #include /* convert 'some-key' to 'someKey' or 'SomeKey'. * the second form is needed for appending to 'set' for 'setSomeKey' */ QString qtify_name(const char *name) { bool next_cap = false; QString result; while (*name) { if (*name == '-') { next_cap = true; } else if (next_cap) { result.append(toupper(*name)); next_cap = false; } else { result.append(*name); } name++; } return result; } /* Convert 'someKey' to 'some-key' * * This is the inverse function of qtify_name, iff qtify_name was called with a * valid gsettings key name (no capital letters, no consecutive dashes). * * Returns a newly-allocated string. */ gchar * unqtify_name(const QString &name) { const gchar *p; QByteArray bytes; GString *str; bytes = name.toUtf8(); str = g_string_new (NULL); for (p = bytes.constData(); *p; p++) { if (isupper(*p)) { g_string_append_c (str, '-'); g_string_append_c (str, tolower(*p)); } else { g_string_append_c (str, *p); } } return g_string_free(str, FALSE); } gsettings-qt-0.1+14.04.20140408/src/util.h0000644000015301777760000000153312320776503020327 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #ifndef __UTIL_H__ #define __UTIL_H__ #include QString qtify_name(const char *name); gchar * unqtify_name(const QString &name); #endif gsettings-qt-0.1+14.04.20140408/src/qconftypes.h0000644000015301777760000000217612320776503021551 0ustar pbusernogroup00000000000000/* * Copyright © 2011 Canonical Limited * * This library is free software: you can redistribute it and/or modify it under the terms of version 3 of the * GNU Lesser General Public License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this program. If not, * see . * * Author: Ryan Lortie */ #ifndef _qconftypes_h_ #define _qconftypes_h_ #include #include QVariant::Type qconf_types_convert(const GVariantType *gtype); GVariant * qconf_types_collect(const GVariantType *gtype, const void *argument); GVariant *qconf_types_collect_from_variant(const GVariantType *gtype, const QVariant &v); void qconf_types_unpack(GVariant *value, void *argument); QVariant qconf_types_to_qvariant(GVariant *value); #endif /* _qconftypes_h_ */ gsettings-qt-0.1+14.04.20140408/src/qgsettings.cpp0000644000015301777760000001031412320776503022072 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #include "qgsettings.h" #include "qconftypes.h" #include "util.h" #include struct QGSettingsPrivate { QByteArray schema_id; QByteArray path; GSettings *settings; static void settingChanged(GSettings *settings, const gchar *key, gpointer user_data); }; void QGSettingsPrivate::settingChanged(GSettings *, const gchar *key, gpointer user_data) { QGSettings *self = (QGSettings *)user_data; Q_EMIT(self->changed(qtify_name(key))); } QGSettings::QGSettings(const QByteArray &schema_id, const QByteArray &path, QObject *parent): QObject(parent) { priv = new QGSettingsPrivate; priv->schema_id = schema_id; priv->path = path; if (priv->path.isEmpty()) priv->settings = g_settings_new(priv->schema_id.constData()); else priv->settings = g_settings_new_with_path(priv->schema_id.constData(), priv->path.constData()); g_signal_connect(priv->settings, "changed", G_CALLBACK(QGSettingsPrivate::settingChanged), this); } QGSettings::~QGSettings() { g_object_unref (priv->settings); delete priv; } QVariant QGSettings::get(const QString &key) const { gchar *gkey = unqtify_name(key); GVariant *value = g_settings_get_value(priv->settings, gkey); QVariant qvalue = qconf_types_to_qvariant(value); g_variant_unref(value); g_free(gkey); return qvalue; } void QGSettings::set(const QString &key, const QVariant &value) { if (!this->trySet(key, value)) qWarning("unable to set key '%s' to value '%s'", key.toUtf8().constData(), value.toString().toUtf8().constData()); } bool QGSettings::trySet(const QString &key, const QVariant &value) { gchar *gkey = unqtify_name(key); bool success = false; /* fetch current value to find out the exact type */ GVariant *cur = g_settings_get_value(priv->settings, gkey); GVariant *new_value = qconf_types_collect_from_variant(g_variant_get_type (cur), value); if (new_value) success = g_settings_set_value(priv->settings, gkey, new_value); g_free(gkey); g_variant_unref (cur); return success; } QStringList QGSettings::keys() const { QStringList list; gchar **keys = g_settings_list_keys(priv->settings); for (int i = 0; keys[i]; i++) list.append(qtify_name(keys[i])); g_strfreev(keys); return list; } QVariantList QGSettings::choices(const QString &qkey) const { gchar *key = unqtify_name (qkey); GVariant *range = g_settings_get_range(priv->settings, key); g_free(key); if (range == NULL) return QVariantList(); const gchar *type; GVariant *value; g_variant_get(range, "(&sv)", &type, &value); QVariantList choices; if (g_str_equal(type, "enum")) { GVariantIter iter; GVariant *child; g_variant_iter_init (&iter, value); while ((child = g_variant_iter_next_value(&iter))) { choices.append(qconf_types_to_qvariant(child)); g_variant_unref(child); } } g_variant_unref (value); g_variant_unref (range); return choices; } void QGSettings::reset(const QString &qkey) { gchar *key = unqtify_name(qkey); g_settings_reset(priv->settings, key); g_free(key); } bool QGSettings::isSchemaInstalled(const QByteArray &schema_id) { GSettingsSchemaSource *source = g_settings_schema_source_get_default (); GSettingsSchema *schema = g_settings_schema_source_lookup (source, schema_id.constData(), TRUE); if (schema) { g_object_unref (schema); return true; } else { return false; } } gsettings-qt-0.1+14.04.20140408/src/qconftypes.cpp0000644000015301777760000003054312320776503022103 0ustar pbusernogroup00000000000000/* * Copyright © 2011 Canonical Limited * * This library is free software: you can redistribute it and/or modify it under the terms of version 3 of the * GNU Lesser General Public License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this program. If not, * see . * * Author: Ryan Lortie */ #include "qconftypes.h" #include /* This file is responsible for conversion between C++ values and GVariant *. * * We use the QVariant type system (but not QVariant) to express the types of native C++ values. This is * necessary because we must expose these types to Qt via the QMetaObject system. * * GSettings uses the GVariant type system to specify the types of the keys in a schema. * * We therefore have a non-injective, non-surjective partial function from GVariant types to QVariant types. * * The non-injectivity is mandated by the fact that the QVariant type system is less expressive than the * GVariant type system when it comes to specifying the sizes of integers. The non-surjectivity is mandated by * the fact that the GVariant type system has less breadth than the QVariant type system (no date, time, font, * etc. types). * * Due to the non-injectivity, we can only ever go in the direction from GVariant to QVariant; all native type * to GVariant conversions must be done in the presence of GVariant type information. For example, we are * unsure if 'Int' is supposed to be a 32 or 16 bit integer. * * The function is partial: not all GVariant types are handled. This can be improved in the future, but for now * it is probably sufficient to handle the common types that are used in 99% of cases. * * GVariant Type Name/Code C++ Type Name QVariant Type Name * -------------------------------------------------------------------------- * boolean b bool QVariant::Bool * byte y char QVariant::Char * int16 n int QVariant::Int * uint16 q unsigned int QVariant::UInt * int32 i int QVariant::Int * uint32 u unsigned int QVariant::UInt * int64 x long long QVariant::LongLong * uint64 t unsigned long long QVariant::ULongLong * double d double QVariant::Double * string s QString QVariant::String * string array* as QStringList QVariant::StringList * byte array ay QByteArray QVariant::ByteArray * * [*] not strictly an array, but called as such for sake of * consistency with the 'a' appearing in the DBus type system * * We provide three functions here: the (one-way) mapping from GVariantType to QVariant::Type, plus two helpers * for converting GVariant to and from native C++ types. The signatures of these helpers are decided by the * fact that they always need to know the GVariant type information (as discussed above). * * The three functions should all be kept completely in sync with each other in terms of what types they support * and how they elect to map those types. Not-reached assertions are used in the two value conversion functions * in the cases that the type conversion function would have failed (ie: that could should be unreachable). */ QVariant::Type qconf_types_convert(const GVariantType *gtype) { switch (g_variant_type_peek_string(gtype)[0]) { case G_VARIANT_CLASS_BOOLEAN: return QVariant::Bool; case G_VARIANT_CLASS_BYTE: return QVariant::Char; case G_VARIANT_CLASS_INT16: return QVariant::Int; case G_VARIANT_CLASS_UINT16: return QVariant::UInt; case G_VARIANT_CLASS_INT32: return QVariant::Int; case G_VARIANT_CLASS_UINT32: return QVariant::UInt; case G_VARIANT_CLASS_INT64: return QVariant::LongLong; case G_VARIANT_CLASS_UINT64: return QVariant::ULongLong; case G_VARIANT_CLASS_DOUBLE: return QVariant::Double; case G_VARIANT_CLASS_STRING: return QVariant::String; case G_VARIANT_CLASS_ARRAY: if (g_variant_type_equal(gtype, G_VARIANT_TYPE_STRING_ARRAY)) return QVariant::StringList; else if (g_variant_type_equal(gtype, G_VARIANT_TYPE_BYTESTRING)) return QVariant::ByteArray; // fall through default: return QVariant::Invalid; } } GVariant *qconf_types_collect(const GVariantType *gtype, const void *argument) { switch (g_variant_type_peek_string(gtype)[0]) { case G_VARIANT_CLASS_BOOLEAN: return g_variant_new_boolean(*(const bool *) argument); case G_VARIANT_CLASS_BYTE: return g_variant_new_byte(*(const char *) argument); case G_VARIANT_CLASS_INT16: return g_variant_new_int16(*(const int *) argument); case G_VARIANT_CLASS_UINT16: return g_variant_new_uint16(*(const unsigned int *) argument); case G_VARIANT_CLASS_INT32: return g_variant_new_int32(*(const int *) argument); case G_VARIANT_CLASS_UINT32: return g_variant_new_uint32(*(const unsigned int *) argument); case G_VARIANT_CLASS_INT64: return g_variant_new_int64(*(const long long *) argument); case G_VARIANT_CLASS_UINT64: return g_variant_new_int64(*(const unsigned long long *) argument); case G_VARIANT_CLASS_DOUBLE: return g_variant_new_double(*(const double *) argument); case G_VARIANT_CLASS_STRING: { const QString& string = *(const QString *) argument; return g_variant_new_string(string.toUtf8()); } case G_VARIANT_CLASS_ARRAY: if (g_variant_type_equal(gtype, G_VARIANT_TYPE_STRING_ARRAY)) { const QStringList& list = *(const QStringList *) argument; GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); Q_FOREACH (const QString& string, list) g_variant_builder_add(&builder, "s", string.toUtf8().constData()); return g_variant_builder_end(&builder); } else if (g_variant_type_equal(gtype, G_VARIANT_TYPE_BYTESTRING)) { const QByteArray& array = *(const QByteArray *) argument; gsize size = array.size(); gpointer data; data = g_memdup(array.data(), size); return g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING, data, size, TRUE, g_free, data); } // fall through default: g_assert_not_reached(); } } void qconf_types_unpack(GVariant *value, void *argument) { switch (g_variant_classify(value)) { case G_VARIANT_CLASS_BOOLEAN: *(bool *) argument = g_variant_get_boolean(value); break; case G_VARIANT_CLASS_BYTE: *(char *) argument = g_variant_get_byte(value); break; case G_VARIANT_CLASS_INT16: *(int *) argument = g_variant_get_int16(value); break; case G_VARIANT_CLASS_UINT16: *(unsigned int *) argument = g_variant_get_uint16(value); break; case G_VARIANT_CLASS_INT32: *(int *) argument = g_variant_get_int32(value); break; case G_VARIANT_CLASS_UINT32: *(unsigned int *) argument = g_variant_get_uint32(value); break; case G_VARIANT_CLASS_INT64: *(long long *) argument = g_variant_get_int64(value); break; case G_VARIANT_CLASS_UINT64: *(unsigned long long *) argument = g_variant_get_uint64(value); break; case G_VARIANT_CLASS_DOUBLE: *(double *) argument = g_variant_get_double(value); break; case G_VARIANT_CLASS_STRING: *(QString *) argument = QString(g_variant_get_string(value, NULL)); break; case G_VARIANT_CLASS_ARRAY: if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) { GVariantIter iter; QStringList list; const gchar *str; g_variant_iter_init (&iter, value); while (g_variant_iter_next (&iter, "&s", &str)) list.append (str); *(QStringList *) argument = list; break; } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) { *(QByteArray *) argument = QByteArray(g_variant_get_bytestring(value)); break; } // fall through default: g_assert_not_reached(); } } QVariant qconf_types_to_qvariant(GVariant *value) { switch (g_variant_classify(value)) { case G_VARIANT_CLASS_BOOLEAN: return QVariant((bool) g_variant_get_boolean(value)); case G_VARIANT_CLASS_BYTE: return QVariant((char) g_variant_get_byte(value)); case G_VARIANT_CLASS_INT16: return QVariant((int) g_variant_get_int16(value)); case G_VARIANT_CLASS_UINT16: return QVariant((unsigned int) g_variant_get_uint16(value)); case G_VARIANT_CLASS_INT32: return QVariant((int) g_variant_get_int32(value)); case G_VARIANT_CLASS_UINT32: return QVariant((unsigned int) g_variant_get_uint32(value)); case G_VARIANT_CLASS_INT64: return QVariant((long long) g_variant_get_int64(value)); case G_VARIANT_CLASS_UINT64: return QVariant((unsigned long long) g_variant_get_uint64(value)); case G_VARIANT_CLASS_DOUBLE: return QVariant(g_variant_get_double(value)); case G_VARIANT_CLASS_STRING: return QVariant(g_variant_get_string(value, NULL)); case G_VARIANT_CLASS_ARRAY: if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) { GVariantIter iter; QStringList list; const gchar *str; g_variant_iter_init (&iter, value); while (g_variant_iter_next (&iter, "&s", &str)) list.append (str); return QVariant(list); } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) { return QVariant(QByteArray(g_variant_get_bytestring(value))); } // fall through default: g_assert_not_reached(); } } GVariant *qconf_types_collect_from_variant(const GVariantType *gtype, const QVariant &v) { switch (g_variant_type_peek_string(gtype)[0]) { case G_VARIANT_CLASS_BOOLEAN: return g_variant_new_boolean(v.toBool()); case G_VARIANT_CLASS_BYTE: return g_variant_new_byte(v.toChar().cell()); case G_VARIANT_CLASS_INT16: return g_variant_new_int16(v.toInt()); case G_VARIANT_CLASS_UINT16: return g_variant_new_uint16(v.toUInt()); case G_VARIANT_CLASS_INT32: return g_variant_new_int32(v.toInt()); case G_VARIANT_CLASS_UINT32: return g_variant_new_uint32(v.toUInt()); case G_VARIANT_CLASS_INT64: return g_variant_new_int64(v.toLongLong()); case G_VARIANT_CLASS_UINT64: return g_variant_new_int64(v.toULongLong()); case G_VARIANT_CLASS_DOUBLE: return g_variant_new_double(v.toDouble()); case G_VARIANT_CLASS_STRING: return g_variant_new_string(v.toString().toUtf8()); case G_VARIANT_CLASS_ARRAY: if (g_variant_type_equal(gtype, G_VARIANT_TYPE_STRING_ARRAY)) { const QStringList list = v.toStringList(); GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE_STRING_ARRAY); Q_FOREACH (const QString& string, list) g_variant_builder_add(&builder, "s", string.toUtf8().constData()); return g_variant_builder_end(&builder); } else if (g_variant_type_equal(gtype, G_VARIANT_TYPE_BYTESTRING)) { const QByteArray array = v.toByteArray(); gsize size = array.size(); gpointer data; data = g_memdup(array.data(), size); return g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING, data, size, TRUE, g_free, data); } // fall through default: return NULL; } } // vim:sw=4 tw=112 gsettings-qt-0.1+14.04.20140408/src/qgsettings.h0000644000015301777760000000665712320776503021556 0ustar pbusernogroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Lars Uebernickel */ #ifndef __QGSETTINGS_H__ #define __QGSETTINGS_H__ #include #include /** * @brief QGSettings provides access to application settings stored with GSettings. * * GSettings does not allow keys to contain anything other than lower-case * characters and dashes. This class converts all keys to camelCase, to make it easier * for people used to Qt's naming convention. */ class QGSettings: public QObject { Q_OBJECT public: /** * @brief Create a QGSettings object for a given schema_id and path. * @param schema_id The id of the schema * @param path If non-empty, specifies the path for a relocatable schema */ QGSettings(const QByteArray &schema_id, const QByteArray &path = QByteArray(), QObject *parent = NULL); ~QGSettings(); /** * @brief Gets the value that is stored at key * @param key The key for which to retrieve the value (in camelCase) * * It is an error if key does not exist in the schema associated with this * QGSettings object. */ QVariant get(const QString &key) const; /** * @brief Sets the value at key to value * @key The key for which to set the value (in camelCase) * @value The value to set * * It is an error if key does not exist in the schema associated with this * QGSettings object. * * Not all values that a QVariant can hold can be serialized into a * setting. Basic types (integers, doubles, strings) and string lists are * supported. */ void set(const QString &key, const QVariant &value); /** * @brief Sets the value at key to value * @key The key for which to set the value * @value The value to set * * Behaves just like ::set(key, value), but returns false instead of * printing a warning if the key couldn't be set. * * @return whether the key was set */ bool trySet(const QString &key, const QVariant &value); /** * \brief Retrieves the list of avaliable keys */ QStringList keys() const; /** * \brief Returns the list of values that key can assume * * Returns an empty list if the schema doesn't contain that information for * the key. */ QVariantList choices(const QString &key) const; /** * @brief Resets the setting for @key to its default value. */ void reset(const QString &key); /** * @brief Checks if a schema with the given id is installed. */ static bool isSchemaInstalled(const QByteArray &schema_id); Q_SIGNALS: /** * \brief Emitted when the value associated with key has changed */ void changed(const QString &key); private: struct QGSettingsPrivate *priv; friend struct QGSettingsPrivate; }; #endif