MeeGo 1.2 Harmattan Developer Documentation Develop for the Nokia N9

Example of storing settings directly to GConf

In addition to managing settings with Control Panel Applets, it is also possible to read and write application settings directly into GConf. This is done with libgq-gconf module in the Platform API, which provides an easy and customisable way to handle GConf calls. In situations where you want to integrate libgq-gconf functionality with a QML based UI, you need to create a small proxy class since the GConf calls cannot be directly accessed from QML. An example proxy class solution is described below.

The following code segments present a GConfItemQmlProxy class. The class has three read/write properties for the GConf key, value and default value. When the value for a key is changed, it is sent to GConf as a signal.

header file

#ifndef GCONFITEMQMLPROXY_H
#define GCONFITEMQMLPROXY_H

#include <QObject>
#include <QVariant>
#include <QString>
#include <gconfitem.h>

class GConfItemQmlProxy : public QObject
{
   Q_OBJECT
   Q_PROPERTY(QString key READ key WRITE setKey)
   Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
   Q_PROPERTY(QVariant defaultValue READ defaultValue WRITE setDefaultValue)

public:
   explicit GConfItemQmlProxy(QObject *parent = 0);

   const QString &key();
   void setKey(const QString &newKey);
   const QVariant &value();
   void setValue(const QVariant &newValue);
   const QVariant &defaultValue();
   void setDefaultValue(const QVariant &newValue);

signals:
   void valueChanged();

public slots:
   void updateValue();

private:
   GConfItem *_gconfItem;
   QString _key;
   QVariant _value;
   QVariant _defaultValue;
};

#endif // GCONFITEMQMLPROXY_H

source file

#include <QDebug>
#include "gconfitemqmlproxy.h"

GConfItemQmlProxy::GConfItemQmlProxy(QObject *parent) :
   QObject(parent)
{
   _gconfItem = NULL;
   _key = "";
}

const QString & GConfItemQmlProxy::key()
{
   return _key;
}

void GConfItemQmlProxy::setKey(const QString &newKey)
{
   if (_gconfItem) {
       delete _gconfItem;
   }
   _gconfItem = new GConfItem(newKey);
   _key = newKey;

   connect(_gconfItem, SIGNAL(valueChanged()), this, SLOT(updateValue()));
}

const QVariant & GConfItemQmlProxy::value()
{
   _value = _gconfItem->value(_defaultValue);
   return _value;
}

void GConfItemQmlProxy::setValue(const QVariant &newValue)
{
   if (_gconfItem) {
       _gconfItem->set(newValue);
       _value = newValue;
   }
}

const QVariant & GConfItemQmlProxy::defaultValue()
{
   return _defaultValue;
}

void GConfItemQmlProxy::setDefaultValue(const QVariant &newValue)
{
   _defaultValue = newValue;
}

void GConfItemQmlProxy::updateValue()
{
   _value = _gconfItem->value(_defaultValue);
   emit valueChanged();
}

Further actions

Similarly to the earlier example where the Control Panel Applet was launched, direct GConf read and write events can also be integrated into the application QML elements. First, make the D-Bus call visible to the QML UI structure in the main.cpp main loop with the following registration:

qmlRegisterType<GConfItemQmlProxy>("MeeGoSettings", 1, 0, "GConfItem");

You can then create a set of QML elements to handle the GConf events. For example, you can read a text string into a Label:

import MeeGoSettings 1.0

GConfItem {
   id: textSetting
   key: "/apps/ControlPanel/MySettingsExample/Message"
   defaultValue: "Default text"
   }

Label {
   id: exampleItem
   text: textSetting.value
   // ...
   }