Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dconf] added MGConfItem::listItems() #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/mgconfitem.cpp
Expand Up @@ -171,6 +171,94 @@ QStringList MGConfItem::listDirs() const
return children;
}

QStringList MGConfItem::listKeys() const
{
QStringList children;
gint length = 0;
QByteArray k = convertKey(priv->key);
if (!k.endsWith("/")) {
k.append("/");
}

gchar **dirs = dconf_client_list(priv->client, k.data(), &length);
GError *error = NULL;

for (gint x = 0; x < length; x++) {
const gchar *dir = g_strdup_printf ("%s%s", k.data(), dirs[x]);
if (dconf_is_key(dir, &error)) {
// We have to mimic how gconf was behaving.
// so we need to chop off trailing slashes.
// dconf will also barf if it gets a "path" with 2 slashes.
QString d = convertKey(dir);
if (d.endsWith("/")) {
d.chop(1);
}

children.append(d);
}

g_free ((gpointer)dir);

// If we have error set then dconf_is_key() has returned false so we should be safe here
if (error) {
qWarning() << "MGConfItem" << error->message;
g_error_free(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - replace these two lines with g_clear_error(&error);

error = NULL;
}
}

g_strfreev(dirs);

return children;
}

QVariantMap MGConfItem::listValues() const
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: Remove empty line here

QVariantMap children;
gint length = 0;
QByteArray k = convertKey(priv->key);
if (!k.endsWith("/")) {
k.append("/");
}

gchar **items = dconf_client_list(priv->client, k.data(), &length);
GError *error = NULL;

for (gint x = 0; x < length; x++) {
const gchar *item = g_strdup_printf ("%s%s", k.data(), items[x]);
if (dconf_is_key(item, &error)) {
QString k = convertKey(item);
QVariant val;
GVariant *v = dconf_client_read(priv->client, item);
if (!v) {
qWarning() << "MGConfItem Failed to read" << priv->key;
val = priv->value;
}

val = MDConf::convertValue(v);

children[k] = val;

if (v) {
g_variant_unref(v);
}
}
g_free ((gpointer)item);

// If we have error set then dconf_is_key() has returned false so we should be safe here
if (error) {
qWarning() << "MGConfItem::listItems()" << error->message;
g_error_free(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next line can be replaced with: g_clear_error(&error);

error = NULL;
}
}

g_strfreev(items);

return children;
}

bool MGConfItem::sync()
{
dconf_client_sync(priv->client);
Expand Down
19 changes: 19 additions & 0 deletions src/mgconfitem.h
Expand Up @@ -122,12 +122,31 @@ class MLITESHARED_EXPORT MGConfItem : public QObject
*/
QStringList listDirs() const;

/*! Return a map of the keys inside this directory. The
returned strings are absolute key names like
"/myapp/settings".

A directory is a key that has children. The same key might
also have a value, but that is confusing and best avoided.
*/

QStringList listKeys() const;

/*! Return a map of the keys with it's values inside this directory.

A directory is a key that has children. The same key might
also have a value, but that is confusing and best avoided.
*/

QVariantMap listValues() const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't listValues() be done (with possibly slightly worse performance if all values are required, but possibly better performance if only some values are required) on the client side by something like:

MGConfItem foo(...);
QVariantMap values;
foreach (const QString &key, foo.listKeys()) {
    values[key] = MGConfItem(key).value();
}

This way, getting only values of interest is easily possible:

MGConfItem foo(...);
QVariantMap values;
foreach (const QString &key, foo.listKeys()) {
    if (key.contains("something_interesting")) {
        values[key] = MGConfItem(key).value();
    }
}

If so, we might want to not provide a listValues() function in the API until we are sure this is needed (basically if there are enough use cases where performance would benefit compared to the loop-over-keys-and-fetch-value construct).


/*! Request dconf to sync value(s) which are not yet synced to the cache.
Sometimes values may not be synced because eventually this just hints
dconf to start sync.

Returns true if there's no errors and false on error.
*/

bool sync();

Q_SIGNALS:
Expand Down