How to use Gconf

 

GConf

GConf is the configuration system of choice for GNOME 2 applications. It is replacing gnome-config, a very simple INI file based format, which turned out to be rather limited for large applications, and for handling advanced system-wide configuration by sysadmins. GConf on the other hand, is a lot more capable in those areas.

GConf was introduced in the GNOME 1.4 platform, but most GNOME applications didn't use it, with the most notable exception being Nautilus, the new GNOME file manager.

For GNOME 2 though, GConf is being rolled in more aggressively, with all (or at least most) of the core applications using GConf to store and manage their configuration data.

Advantages of GConf

There are numerous advantages to using GConf. The configuration data is stored in a tree structure, which makes it a lot easier to manage the preferences of a large application, which might have many preferences and configuration options.

An example of this tree structured configuration data might look like the following, taken from the web browser Galeon:

/apps/galeon:
/apps/galeon/Rendering:
/apps/galeon/Rendering/FontsColors:
background_color = #FFFFFF
visited_link_color = #FF0000

Here you see that Galeon is storing its preferences under the apps category, which is the place for applications to do so. Under the desktop category, on the other hand, desktop wide preferences, such as whether to use a left or right handed mouse, are stored.

Using subcategories, the preferences are split in small groups that belong together. This makes things clearer than just keeping all the options in one huge list, like the old-fashioned way dictated by gnome-config.

Data types

The kinds of data you can store in a GConf entry are: integer, string, float, boolean, schema, list, pair.

Most of these should be familiar to you, perhaps with the exception of schema, list and pair.

Schemas store a GConfSchema data type, which contains meta-information about a key. This will be described a bit more in detail further on in the article.

The list type stores a list of keys, although it has a few limitations. First all the elements must be of the same type, second, only the primitive types can be contained in a list (that is, all the types except list and pair).

Pairs can store two primitive values, either of the same type, or two different ones. Pairs can, like lists, only contain primitive types.

Notification

GConf offers another very nice feature for the modern desktop, namely notification across applications. This makes it possible for multiple applications, or multiple running instances of the same application, to immediately react to changes to preferences, and perform the necessary work to adapt to the new preferences.

In order to get notified of such a change, your application must tell GConf that it wants to listen to changes to a certain value, or to a whole directory in the configuration database.

Let's try all this out with a small example:(gconf-test.c)

Makefile:

This example will popup a small window with a check button, that is connected to a GConf key. Try compiling the program, and start two instances of it, to see the notification service at work. When you check the button in one of the programs, it will immediately update the second program window as well! Also note that the state of the button is saved so the next time you start the program, it will be the same way as the last time you ran it.

 

Writing and using schema files

To use schemas with your application, you first need to create a schema file, which is an XML file, listing all the schemas. A simple example might look like:

As you can see, a schema file is simply a list of schemas. For each schema, you specify the type, default value, and which key the schema applies to. Make sure to add useful descriptions of the key, both the short and the long versions.

When you have your schema file, you can use gconftool to install it:

# gconftool --install-schema-file=FILENAME

If you want to read more about GConf, check out the API reference at: http://developer.gnome.org/doc/API/gconf/index.html .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值