./0000755000004100000410000000000013147604062011246 5ustar www-datawww-data./GSettings/0000755000004100000410000000000013147604062013155 5ustar www-datawww-data./GSettings/gsettings-qml.h0000644000004100000410000000421313147604062016124 0ustar www-datawww-data/* * 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 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/qmldir0000644000004100000410000000010513147604062014364 0ustar www-datawww-datamodule GSettings plugin GSettingsQmlPlugin typeinfo plugins.qmltypes ./GSettings/plugin.cpp0000644000004100000410000000207013147604062015156 0ustar www-datawww-data/* * 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/gsettings-qml.cpp0000644000004100000410000001107213147604062016460 0ustar www-datawww-data/* * 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); // make sure this object gets the new value immediately and not on the // next main loop iteration (see updateValue) parent->settingChanged(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(const 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); if (this->value(key) != value) { 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)) { // due to QTBUG-32859, QGSettings doesn't dispatch its changed signal // directly, but on a new main loop iteration. At that point, this // object already has the new value set and doesn't emit its own // changed signal (see ::settingChanged). Emit it here so that it is // sent even when the setting is changed from qml. Q_EMIT(changed(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/plugin.h0000644000004100000410000000160713147604062014630 0ustar www-datawww-data/* * 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/gsettings-qt.pro0000644000004100000410000000141113147604062016325 0ustar www-datawww-dataTEMPLATE = 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 ./COPYING0000644000004100000410000001674313147604062012314 0ustar www-datawww-data 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. ./src/0000755000004100000410000000000013147604062012035 5ustar www-datawww-data./src/util.h0000644000004100000410000000153313147604062013165 0ustar www-datawww-data/* * 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 ./src/qgsettings.cpp0000644000004100000410000001151113147604062014730 0ustar www-datawww-data/* * 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; GSettingsSchema *schema; gulong signal_handler_id; 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; // work around https://bugreports.qt.io/browse/QTBUG-32859 and http://pad.lv/1460970 QMetaObject::invokeMethod(self, "changed", Qt::QueuedConnection, Q_ARG(QString, 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_object_get (priv->settings, "settings-schema", &priv->schema, NULL); priv->signal_handler_id = g_signal_connect(priv->settings, "changed", G_CALLBACK(QGSettingsPrivate::settingChanged), this); } QGSettings::~QGSettings() { if (priv->schema) { g_settings_sync (); g_signal_handler_disconnect(priv->settings, priv->signal_handler_id); g_object_unref (priv->settings); g_settings_schema_unref (priv->schema); } 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); GSettingsSchemaKey *schema_key = g_settings_schema_get_key (priv->schema, key); GVariant *range = g_settings_schema_key_get_range(schema_key); g_settings_schema_key_unref (schema_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_settings_schema_unref (schema); return true; } else { return false; } } ./src/QGSettings0000644000004100000410000000003013147604062014001 0ustar www-datawww-data#include "qgsettings.h" ./src/qconftypes.h0000644000004100000410000000217613147604062014407 0ustar www-datawww-data/* * 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_ */ ./src/qgsettings.h0000644000004100000410000000665713147604062014414 0ustar www-datawww-data/* * 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 ./src/qconftypes.cpp0000644000004100000410000002301213147604062014732 0ustar www-datawww-data/* * 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 * dictionary a{ss} QVariantMap QVariant::Map * * [*] 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; else if (g_variant_type_equal(gtype, G_VARIANT_TYPE ("a{ss}"))) return QVariant::Map; // fall through default: return QVariant::Invalid; } } 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))); } else if (g_variant_is_of_type(value, G_VARIANT_TYPE("a{ss}"))) { GVariantIter iter; QMap map; const gchar *key; const gchar *val; g_variant_iter_init (&iter, value); while (g_variant_iter_next (&iter, "{&s&s}", &key, &val)) map.insert(key, QVariant(val)); return map; } // 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); } else if (g_variant_type_equal(gtype, G_VARIANT_TYPE("a{ss}"))) { GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE ("a{ss}")); QMapIterator it(v.toMap()); while (it.hasNext()) { it.next(); QByteArray key = it.key().toUtf8(); QByteArray val = it.value().toByteArray(); g_variant_builder_add (&builder, "{ss}", key.constData(), val.constData()); } return g_variant_builder_end (&builder); } // fall through default: return NULL; } } // vim:sw=4 tw=112 ./src/gsettings-qt.pro0000644000004100000410000000127413147604062015214 0ustar www-datawww-dataTEMPLATE = 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 ./src/util.cpp0000644000004100000410000000375513147604062013530 0ustar www-datawww-data/* * 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(QChar(*name).toUpper().toLatin1()); 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++) { const QChar c(*p); if (c.isUpper()) { g_string_append_c (str, '-'); g_string_append_c (str, c.toLower().toLatin1()); } else { g_string_append_c (str, *p); } } return g_string_free(str, FALSE); } ./tests/0000755000004100000410000000000013147604062012410 5ustar www-datawww-data./tests/cpptest.cpp0000644000004100000410000000175613147604062014607 0ustar www-datawww-data#include #include class TestDeferredDelete: public QObject { Q_OBJECT private slots: void initTestCase(); void test_deferredDelete(); private: QGSettings * settings; QPointer dummy; }; void TestDeferredDelete::initTestCase() { settings = new QGSettings("com.canonical.gsettings.Test", QByteArray(), this); dummy = new QObject; connect(settings, &QGSettings::changed, dummy.data(), &QObject::deleteLater); // delete the dummy object upon any gsettings change } void TestDeferredDelete::test_deferredDelete() { QSignalSpy spy(dummy.data(), &QObject::destroyed); // watch the dummy object get destroyed settings->set("testString", "bar"); QVERIFY(spy.wait(1)); QVERIFY(dummy.isNull()); // verify dummy got destroyed for real QCOMPARE(settings->get("testString").toString(), QStringLiteral("bar")); // also verify the setting got written by reading it back } QTEST_MAIN(TestDeferredDelete) #include "cpptest.moc" ./tests/com.canonical.gsettings.test.gschema.xml0000644000004100000410000000157313147604062022236 0ustar www-datawww-data 42 1.5 false "hello" 'one' ['one', 'two', 'three'] {'foo': 'one', 'bar': 'two'} ./tests/cpptest.pro0000644000004100000410000000063013147604062014613 0ustar www-datawww-dataTEMPLATE = app QT += testlib QT -= gui CONFIG += testcase link_pkgconfig TARGET = cpptest IMPORTPATH = $$PWD/.. SOURCES = cpptest.cpp INCLUDEPATH += $$(PWD)/../src LIBS += -L$$(PWD)/../src -lgsettings-qt 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 ./tests/tst_GSettings.qml0000644000004100000410000000572413147604062015734 0ustar www-datawww-data 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" // has to be "valueChanged" signal, not "changed"; the latter doesn't work reliably with the in-memory gsettings backend onValueChanged: changes.push([key, value]); } SignalSpy { id: changesSpy target: settings signalName: "changed" } 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(settings.testMap, {'foo': 'one', 'bar': 'two'}); 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.testMap = {'baz': 'three'} compare(settings.testMap, {'baz': 'three'}); settings.testMap = {}; compare(settings.testMap, {}); 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; changesSpy.clear(); settings.schema.reset('testInteger'); compare(settings.testInteger, 42); tryCompare(changesSpy, "count", 1); } function test_invalid_schema() { compare(invalid_settings.schema.isValid, false); compare(invalid_settings.schema.testInteger, undefined); } } ./tests/test.cpp0000644000004100000410000000007713147604062014077 0ustar www-datawww-data#include QUICK_TEST_MAIN(GSettings) ./tests/manual0000644000004100000410000000134413147604062013612 0ustar www-datawww-data Test-case gsettings-qt/unity8-check-setting
Open system settings with the security panel
Ensure "Dash search" is set to "Phone and Internet"
Change "Dash search" to "Phone only"
Ensure that the music and video lenses don't show online content
Test-case gsettings-qt/run-example
Download the choices example and run it with qmlscene
Ensure that the switch is synchronized with the "Dash Search" setting in the security panel of system settings
Ensure that the "Possible values" list item has a value of "all, none"
./tests/tests.pro0000644000004100000410000000054513147604062014300 0ustar www-datawww-dataTEMPLATE = 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 OTHER_FILES += tst_GSettings.qml ./gsettings-qt.pro0000644000004100000410000000016013147604062014416 0ustar www-datawww-dataTEMPLATE = subdirs SUBDIRS += src/gsettings-qt.pro GSettings/gsettings-qt.pro tests/tests.pro tests/cpptest.pro ./examples/0000755000004100000410000000000013147604062013064 5ustar www-datawww-data./examples/choices.qml0000644000004100000410000000117513147604062015220 0ustar www-datawww-data import GSettings 1.0 import QtQuick 2.0 import Ubuntu.Components 0.1 import Ubuntu.Components.ListItems 0.1 as ListItems Item { width: 400 height: 300 GSettings { id: settings schema.id: "com.canonical.Unity.Lenses" } Column { anchors.fill: parent ListItems.Standard { text: 'Dash search' control: Switch { checked: settings.remoteContentSearch == 'all' onClicked: settings.remoteContentSearch = checked ? 'all' : 'none' } } ListItems.SingleValue { text: 'Possible values' value: settings.schema.choices('remoteContentSearch').join(', ') } } }