refactor: classname rename, minor documentation

This commit is contained in:
Gary Wang 2019-08-07 17:51:36 +08:00
parent f6abd96c77
commit a86075b843
3 changed files with 77 additions and 31 deletions

View File

@ -25,15 +25,15 @@
#include <QObject> #include <QObject>
#include <QScopedPointer> #include <QScopedPointer>
class DGIOSettingsPrivate; class DGioSettingsPrivate;
class DGIOSettings : public QObject class DGioSettings : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit DGIOSettings(const QString& schemaId, QObject* parent = nullptr); explicit DGioSettings(const QString& schemaId, QObject* parent = nullptr);
DGIOSettings(const QString& schemaId, const QString& path, QObject* parent = nullptr); DGioSettings(const QString& schemaId, const QString& path, QObject* parent = nullptr);
~DGIOSettings(); ~DGioSettings();
void sync(); void sync();
@ -50,9 +50,9 @@ Q_SIGNALS:
void valueChanged(const QString& key, const QVariant& value); void valueChanged(const QString& key, const QVariant& value);
private: private:
QScopedPointer<DGIOSettingsPrivate> d_private; QScopedPointer<DGioSettingsPrivate> d_private;
Q_DECLARE_PRIVATE_D(d_private, DGIOSettings) Q_DECLARE_PRIVATE_D(d_private, DGioSettings)
}; };
#endif // DGIOSETTINGS_H #endif // DGIOSETTINGS_H

View File

@ -170,17 +170,17 @@ static GVariant *qconf_types_collect_from_variant(const GVariantType* gtype, con
return nullptr; return nullptr;
} }
class DGIOSettingsPrivate class DGioSettingsPrivate
{ {
public: public:
DGIOSettingsPrivate(DGIOSettings* qq) DGioSettingsPrivate(DGioSettings* qq)
: ptr(qq) : ptr(qq)
{ {
} }
QString schemaId; QString schemaId;
QString path; QString path;
DGIOSettings* ptr; DGioSettings* ptr;
GSettings* settings; GSettings* settings;
GSettingsSchema* schema; GSettingsSchema* schema;
gulong signalHandlerId; gulong signalHandlerId;
@ -219,20 +219,39 @@ public:
} }
static void onSettingChanged(GSettings* settings, const gchar* key, gpointer pointer) { static void onSettingChanged(GSettings* settings, const gchar* key, gpointer pointer) {
DGIOSettingsPrivate* self = static_cast<DGIOSettingsPrivate*>(pointer); DGioSettingsPrivate* self = static_cast<DGioSettingsPrivate*>(pointer);
Q_EMIT self->ptr->valueChanged(key, self->value(settings, key)); Q_EMIT self->ptr->valueChanged(key, self->value(settings, key));
} }
}; };
/*!
* \class DGioSettings
* \brief Access GSettings in a more Qt way.
*
* This is not a directly wrapper of Gio::Settings class, so interfaces are not matched to the
* giomm ome.
*/
DGIOSettings::DGIOSettings(const QString& schemaId, QObject* parent) /*!
: DGIOSettings(schemaId, QString(), parent) * \brief Create a DGioSettings object for a given \a schemaId.
*
* In case you would like to skip path but provide a \a parent.
*
* Warning: not existed schemaId will cause the program crashed.
*/
DGioSettings::DGioSettings(const QString& schemaId, QObject* parent)
: DGioSettings(schemaId, QString(), parent)
{ {
} }
DGIOSettings::DGIOSettings(const QString& schemaId, const QString& path, QObject* parent) /*!
* \brief Create a DGioSettings object for a given \a schemaId and \a path.
*
* Warning: not existed schemaId will cause the program crashed.
*/
DGioSettings::DGioSettings(const QString& schemaId, const QString& path, QObject* parent)
: QObject(parent) : QObject(parent)
, d_private(new DGIOSettingsPrivate(this)) , d_private(new DGioSettingsPrivate(this))
{ {
d_private->schemaId = schemaId; d_private->schemaId = schemaId;
d_private->path = path; d_private->path = path;
@ -243,12 +262,12 @@ DGIOSettings::DGIOSettings(const QString& schemaId, const QString& path, QObject
path.toUtf8().constData()); path.toUtf8().constData());
g_object_get(d_private->settings, "settings-schema", &d_private->schema, nullptr); g_object_get(d_private->settings, "settings-schema", &d_private->schema, nullptr);
d_private->signalHandlerId = g_signal_connect(d_private->settings, "changed", G_CALLBACK(DGIOSettingsPrivate::onSettingChanged), d_ptr.data()); d_private->signalHandlerId = g_signal_connect(d_private->settings, "changed", G_CALLBACK(DGioSettingsPrivate::onSettingChanged), d_ptr.data());
} }
DGIOSettings::~DGIOSettings() DGioSettings::~DGioSettings()
{ {
Q_D(DGIOSettings); Q_D(DGioSettings);
if (d->schema) { if (d->schema) {
g_settings_sync (); g_settings_sync ();
@ -258,9 +277,27 @@ DGIOSettings::~DGIOSettings()
} }
} }
bool DGIOSettings::setValue(const QString& key, const QVariant& value, bool sync) /*!
* \brief Sets the value at \a key to \a value
*
* Not all values that a QVariant can hold can be serialized into a setting. Basic types are supported. the
* provided variant value will be converted according to the original GSetting value type.
*
* List of supported types:
*
* - boolean
* - byte/char
* - int, uint, long long
* - double
* - string
* - string list.
*
* \param sync call sync() after value applied if success.
* \return true if success, false if failed.
*/
bool DGioSettings::setValue(const QString& key, const QVariant& value, bool sync)
{ {
Q_D(DGIOSettings); Q_D(DGioSettings);
if (!d->trySet(key, value)) { if (!d->trySet(key, value)) {
qWarning() << QString("unable to set key %1 to value %2").arg(key).arg(value.toString()); qWarning() << QString("unable to set key %1 to value %2").arg(key).arg(value.toString());
@ -274,16 +311,25 @@ bool DGIOSettings::setValue(const QString& key, const QVariant& value, bool sync
return true; return true;
} }
QVariant DGIOSettings::value(const QString& key) const QVariant DGioSettings::value(const QString& key) const
{ {
Q_D(const DGIOSettings); Q_D(const DGioSettings);
return d->value(d->settings, key); return d->value(d->settings, key);
} }
QStringList DGIOSettings::keys() const /*!
* \brief Retrieves the list of avaliable keys
*
* According to g_settings_list_keys() document: You should probably not be calling this function
* from "normal" code (since you should already know what keys are in your schema). This function
* is intended for introspection reasons.
*
* Thus we also mark this function as DEPRECATED just like what glib does.
*/
QStringList DGioSettings::keys() const
{ {
Q_D(const DGIOSettings); Q_D(const DGioSettings);
QStringList list; QStringList list;
gchar** keys = g_settings_list_keys(d->settings); gchar** keys = g_settings_list_keys(d->settings);
@ -297,21 +343,21 @@ QStringList DGIOSettings::keys() const
return list; return list;
} }
void DGIOSettings::reset(const QString& key) void DGioSettings::reset(const QString& key)
{ {
Q_D(DGIOSettings); Q_D(DGioSettings);
g_settings_reset(d->settings, key.toUtf8().constData()); g_settings_reset(d->settings, key.toUtf8().constData());
} }
void DGIOSettings::sync() void DGioSettings::sync()
{ {
Q_D(DGIOSettings); Q_D(DGioSettings);
d->sync(); d->sync();
} }
bool DGIOSettings::isSchemaInstalled(const QString& schemaId) bool DGioSettings::isSchemaInstalled(const QString& schemaId)
{ {
GSettingsSchemaSource* source = g_settings_schema_source_get_default(); GSettingsSchemaSource* source = g_settings_schema_source_get_default();

View File

@ -13,12 +13,12 @@ public:
private Q_SLOTS: private Q_SLOTS:
void testCase_Settings() { void testCase_Settings() {
if (DGIOSettings::isSchemaInstalled("com.deepin.dde.appearance") == false) { if (DGioSettings::isSchemaInstalled("com.deepin.dde.appearance") == false) {
qWarning() << "com.deepin.dde.appearance" << "invalid"; qWarning() << "com.deepin.dde.appearance" << "invalid";
return; return;
} }
DGIOSettings settings("com.deepin.dde.appearance", this); DGioSettings settings("com.deepin.dde.appearance", this);
QVERIFY(settings.value("background-uris").isValid()); QVERIFY(settings.value("background-uris").isValid());
QVERIFY(settings.value("extra-picture-uris").isValid()); QVERIFY(settings.value("extra-picture-uris").isValid());
const QStringList& tmpValue = settings.value("extra-picture-uris").toStringList(); const QStringList& tmpValue = settings.value("extra-picture-uris").toStringList();