Back to Index

Maemo for mobile developers

Managing application information with GConf

Table of contents

  1. Introduction: Gconf
  2. Gconfmm example application

Introduction: Gconf

GConf provides a simple way for applications to store information such as settings, preferences, and other data. It is also used by the system for system-wide configuration settings management.

For more information on GConf, click here.

GConfmm example application

This section goes through a simple example on how to use some GConf features in a C++ application. This application stores the values of a text entry and a check button and retrieves the stored information when the application is restarted.

main.cpp:

Start by creating a main.cpp file, which is very similar to one in the previous examples. There are just two changes; one is including the gconfmm.h header file, and the other is to initialize the GConf libraries with Gnome::Conf::init().

#include "config.h"

#include <gconfmm.h>
#include <hildonmm.h>
#include <iostream>

#include "mywindow.h"

int main(int argc, char *argv[])
{
        /* Initialize gtkmm and checks the arguments passed to your application */
        Gnome::Conf::init();
	Gtk::Main main_loop(&argc, &argv);

        ...
}

mywindow.h:

Create the MyWindow class, which is the main window of the application. The header files related to the features used in the application are included in the mywindow.h file. The GConf classes and the widgets that are added to the window are also declared.

#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <gconfmm.h>
#include <glibmm.h>
#include <gtkmm.h>
#include <hildonmm.h>

class MyWindow : public Hildon::Window
{
public:
        MyWindow();
        virtual ~MyWindow();
	void restore();

protected:
        virtual void on_save_clicked();
	virtual void on_menu_quit();

        Gtk::Menu m_main;
	Gtk::VBox vbox;
        Gtk::HBox hbox1;
        Gtk::HBox hbox2;
        Gtk::HBox hbox3;
        Gtk::CheckButton m_checkbutton;
        Gtk::Label m_label;
        Gtk::Entry m_entry;
        Gtk::Button m_save;

        Glib::RefPtr<Gnome::Conf::Client> gconf;
        std::auto_ptr<Glib::Error> error;

        Glib::ustring gconf_textentry;
        Glib::ustring gconf_checkbox;
};

#endif /* MYWINDOW_H */

mywindow.cpp:

Implement the MyWindow class in mywindow.cpp.

#include <gconfmm.h>
#include <glibmm.h>
#include <gtkmm.h>
#include <hildonmm.h>

#include "mywindow.h"

In the class constructor, define besides the string's initialization also where GConf should store the application information. Also initialize the GConf handler, so the application can write/read information on it.

/* Class constructor */
MyWindow::MyWindow():
        m_checkbutton ("Check Button State"),
        m_label ("Text Box Entry"),
        m_save("Save"),
	m_item_exit("Exit"),
        gconf_textentry("/apps/gconf_example/textentry"),    /* Define where GConf will store the text entry */
        gconf_checkbox("/apps/gconf_example/checkbox")       /* Define where GConf will store the check box state */
{
        /* Add items to the menu */
        m_main.append(m_item_exit);
        m_main.show_all();
	set_main_menu(m_main);

	/* Create the widgets */
        add(vbox);
	vbox.set_border_width(50);

        vbox.pack_start(hbox1);
        vbox.pack_start(hbox2);
	vbox.pack_start(hbox3);

        hbox1.pack_start(m_label);
        hbox1.pack_start(m_entry);
        hbox2.pack_start(m_checkbutton);
	hbox3.pack_start(m_save);

        /* Returns the GConf handler */ 
	gconf = Gnome::Conf::Client::get_default_client();

	/* Attach the callback functions to the activate signal */
        m_save.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_save_clicked));
	m_item_exit.signal_activate().connect(sigc::mem_fun(*this, &MyWindow::on_menu_quit));

	/* Call the method to load the application information */
	restore();

        /* Show all widgets on the screen */
        show_all();                                           
}

Call the on_menu_quit() method to close the application.

/* Callback function, which will be called to close the application */
void MyWindow::on_menu_quit()
{
        hide();
}

The on_save_clicked() method is called when the Save button is clicked. It stores the text entry content and the check button state on GConf.

/* Save current status */
void MyWindow::on_save_clicked()
{
        /* Get the check button state and store it on GConf */
        if (m_checkbutton.get_active())     
                gconf->set(gconf_checkbox, true, error);
        else
                gconf->set(gconf_checkbox, false, error);

        /* Store the text entry content on GConf */
        gconf->set(gconf_textentry, m_entry.get_text(), error);
}

The restore() method loads the stored application information and sets the respective widgets.

/* Load stored information */
void MyWindow::restore()
{
        /* Get the stored check button state and set the widget accordingly */
        if (gconf->get_bool(gconf_checkbox, error))
                m_checkbutton.set_active();

        /* Get the stored text entry content and set the widget accordingly */
        m_entry.set_text(gconf->get_string(gconf_textentry, error));
}

Now build the application. The following screen is displayed:
Gconfmm

The source code for this example is available here

Network programming using sockets