基于 GNOME2 桌面的 GConf 配置系统的介绍

转载自:

https://www.ibm.com/developerworks/cn/linux/l-cn-gconf/

 

概述

GConf 是在基于 GNOME2 的 Linux 操作系统中实现对应用程序的配置及管理功能的工具。我们可以把 GConf 理解为 Linux 操作系统中的注册表。然而,它克服了 Windows 注册表的一些缺点,比如 Windows 注册表遭到破坏,可能会导致操作系统崩溃,而且 GConf 的配置信息存储于纯文本的文件中,可读性很好。从根本上来说,GConf 采用用一种 Key/Value 的存储机制。在 GConf 系统中,应用程序的配置信息都是以 Key/Value 的结构来存储。每一个 Key 值对应应用程序的某种属性,而对应的 Value 则表示该属性的配置信息。 GConf 在后台实现了一个用户配置信息的数据库,这个数据库看上去类似一个文件系统,专门用于存储应用程序的 Key/Value 信息。整个文件系统主要有以下组件构成。包括目录(对应使用 GConf 系统的应用程序。如 /apps/evolution),子目录(一系列属性配置信息的集合。如 /apps/evolution/mail),/schemas(存储属性的键信息)等。

GConf 系统还可以在一个应用程序的属性配置值发生改变的时候通知应用程序。因此,当 GConf 中一个属性值被更改的时候,与之相关的应用程序都能够获得信息,从而根据属性的修改更新作出相应的处理。GConf 主要由三个组件构成:

  • 一系列用户属性的配置集合
  • 一个后台程序 gconfd-2
  • 一个命令行工具 gconftool-2

另外 Gnome 系统还有一个可视化的图形工具 gconf-editor 供用户使用。

 

基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


Gconf 配置源介绍

GConf 系统中保存了一系列文件路径和位置信息,我们称之为配置源。这些配置源信息存储在GConf 的一个路径文件中。在笔者的 SUSE Linux 系统,该文件为 /etc/opt/gnome/gconf/2/path. (不同的 Linux 发行版本可能略有不同) 。该文件的内容(滤除注释信息)如下:

xml:readonly:/etc/opt/gnome/gconf/gconf.xml.mandatory
include /etc/opt/gnome/gconf/2/local-mandatory.path
include "$(HOME)/.gconf.path"
xml:readwrite:$(HOME)/.gconf
include /etc/opt/gnome/gconf/2/local-defaults.path
xml:readonly:/etc/opt/gnome/gconf/gconf.xml.defaults

 

GConf 系统中的配置源分为三类

强制源( Mandatory :该源所描述的属性为只读,用户无法修改该类属性的值。

xml:readonly:/etc/opt/gnome/gconf/gconf.xml.mandatory
include /etc/opt/gnome/gconf/2/local-mandatory.path

 

用户源( User :该源存储了用户可以自行修改和设置的属性信息。

include "$(HOME)/.gconf.path"
xml:readwrite:$(HOME)/.gconf

 

默认源( Default :该源存储了 GConf 系统中属性的默认值。

include /etc/opt/gnome/gconf/2/local-defaults.path
xml:readonly:/etc/opt/gnome/gconf/gconf.xml.defaults

 

上述源的顺序恰好是 GConf 系统读取应用程序配置属性值的顺序,也就是说存储在强制源中的属性会被优先获取,即使用户源中对该属性进行了自定义配置也会被忽略。

 

基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


GConf Schema 简介

GConf Schema 是一系列键和实体的集合。我们总可以将一个 schema 的键值和一个属性的键值联系在一起。例如我们打开一个关于 FTP 协议相关的 GConf 属性文件:

cbz: /etc/opt/gnome/gconf/gconf.xml.defaults/desktop/gnome/url-handlers/ftp$cat %gconf.xml
<?xml version="1.0"?>
<gconf>
<entry name="needs_terminal" mtime="1165354500" 
     schema="/schemas/desktop/gnome/url-handlers/ftp/needs_terminal"/>
<entry name="command" mtime="1165354500" 
         schema="/schemas/desktop/gnome/url-handlers/ftp/command"/>
<entry name="enabled" mtime="1165354500" 
          schema="/schemas/desktop/gnome/url-handlers/ftp/enabled"/>
</gconf>

 

从中可以看出:

/desktop/gnome/url-handlers/ftp/command

 

对应的 schema 文件中的 Key 为

/schemas/desktop/gnome/url-handlers/ftp/command。

 

下面我们给出部分对应的 schema 文件的信息

/etc/opt/gnome/gconf/schemas/desktop_gnome_url_handlers.schemas 

 <schema>
      <key>/schemas/desktop/gnome/url-handlers/ftp/command</key>
      <applyto>/desktop/gnome/url-handlers/ftp/command</applyto>
      <owner>gnome</owner>
      <type>string</type>
      <default>firefox %s</default>
      <locale name="C">
        <short>The handler for "ftp" URLs</short>
        <long>The command used to handle "ftp" URLs, if enabled.</long>
      </locale>
</schema>


基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


GConf 图形化工具介绍

用 户在终端中输入 gconf-editor。 GConf 的图形化工具将出现在用户面前(见下图1)。gconf-editor 提供给用户一个方便,快捷的可视化终端,从而实现对 GConf 系统中应用程序的属性进行直观地查看,编辑和修改。它的结构看上去和 Windows 操作系统的注册表类似,然而可读性更好。用户能够清楚的查到相关应用程序的配置信息。例如 /apps/gnome-terminal/profiles/Default/font 存储的是用户的 gnome-terminal 终端的使用的字体信息。在本文的示例,用户当前 gnome-terminal 终端的字体为 Bitstream Vera Sans Mono 14. 用户还可以看到关于该 Key 的描述信息如下:

Key name:	         /apps/gnome-terminal/profiles/Default/font  	//键名		
Key owner:	         gnome-terminal					//所属应用程序	
Short Description:	Font					         //简单描述信息	
Long Description:	A Pango font name.  			         //详细描述信息	

 

另外 GConf 系统中还存储了一些关于桌面系统的配置信息,例如在 /desktop/gnome/url-handlers/http/command 存储的是系统处理基于 HTTP 的 URL 的应用程序信息,本文中的使用的是 firefox 。当用户在打开一个含有 HTTP URL 的文档时,如果用户点击其中的 URL 链接,系统会启动 firefox 来打开这个 URL。

Key name:		/desktop/gnome/url-handlers/http/command  
Key owner:		gnome
Short Description:	The handler for “http” URLs
Long Description:	The command to used to handle “http” URLS, if enabled.

 

另外用户可以直接通过 gconf-editor 来修改其中一些配置的信息,例如用户可以修改/desktop/gnome/url-handlers/http/command 的值,从而使得用户在打开 HTTP URL 的时候,能够启动自己定义的网页浏览器。


图1 gconf-editor 示例
基于 GNOME2 桌面的 GConf 配置系统的介绍

基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


GConf 命令行工具介绍

GConf 配置系统自带了一个强大的命令行工具叫作 gconftool-2。 用户可以通过这个工具实现对 GConf 系统进行配置和管理等。用户可以通过 man gconftool-2 获得关于 gconftool-2 命令的详细使用方法,这里我们给出一些常用的命令选项:


gconftool-2 命令选项

命令选项 注释
-s, --set 设置键值并同步。需要与 —type 同时使用
-g, --get 打印一个键值到标准屏幕输出
-u, --unset 重置 Key 值为 GConf 系统中的默认值
-a, --all-entries 打印目录中的所有键/值组合
--all-dirs 打印一个目录的所有子目录
-R, --recursive-list 递归打印一个目录的子目录与条目
--dir-exists=STRING 返回目录是否存在。0为目录存在,2为不存在
--set-schema 设置一个 schema 的属性,与 --short-desc, --long-desc, --owner, 和 –type 同时使用
--makefile-install-rule 安装 schema 文件到 GConf 中。GCONF_CONFIG_SOURCE 环境变量必须设置为相应的配置源
--makefile-uninstall-rule 卸载 schema 文件。与 --makefile-install-rule 相同,GCONF_CONFIG_SOURCE 环境变量必须设置为相应的配置源

 

命令行使用示例:

  1. 递归打印 /desktop/gnome/url-handlers 目录中的子目录和选项
cbz: ~$gconftool-2 --recursive-list  /desktop/gnome/url-handlers

/desktop/gnome/url-handlers/cdda:
  command = /opt/gnome/lib/gnome-volume-manager/gnome-cdda-handler %s
  enabled = false
 /desktop/gnome/url-handlers/man:
  needs_terminal = false
  command = gnome-help "%s"
  enabled = true
  其余略去………

 

  1. 检查目录在 GConf 系统中是否存在
cbz: ~$gconftool-2 --dir-exists=/desktop/gnome/url-handlers
cbz: ~$echo $?
0

 

  1. 列举 /desktop/gnome/url-handlers 的所有子目录
cbz: ~$gconftool-2 --all-dirs /desktop/gnome/url-handlers

 /desktop/gnome/url-handlers/cdda
 /desktop/gnome/url-handlers/man
 /desktop/gnome/url-handlers/aim
 /desktop/gnome/url-handlers/ftp
 /desktop/gnome/url-handlers/info
其余略去………

 

  1. 打印 /desktop/gnome/url-handlers/https/command 的值
cbz: ~$gconftool-2 -g /desktop/gnome/url-handlers/https/command
firefox %s 

 

  1. 设置 /desktop/gnome/url-handlers/https/command 对应的值为 ”test”
cbz: ~$gconftool-2 --type string -s /desktop/gnome/url-handlers/https/command test
cbz: ~$gconftool-2 -g /desktop/gnome/url-handlers/https/command
test

 

  1. 设置 /desktop/gnome/url-handlers/https/command 对应的值为系统默认值
cbz: ~$gconftool-2 --unset /desktop/gnome/url-handlers/https/command
cbz: ~$gconftool-2 -g /desktop/gnome/url-handlers/https/command
firefox %s

 

  1. 安装一个自定义的 schema 文件到系统中,安装源为当前用户个人目录
cbz: ~$export GCONF_CONFIG_SOURCE="xml:readwrite:$HOME/.gconf"
cbz: ~$gconftool-2 --makefile-install-rule Test.schemas

 

  1. 卸载一个自定义的 schema 文件,卸载源为当前用户个人目录
cbz: ~$export GCONF_CONFIG_SOURCE=xml:readwrite:$HOME/.gconf
cbz: ~$gconftool-2 --makefile-uninstall-rule Test.schemas     


基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


利用 GConf API 编程示例

GConf 系统本身提供了许多 API 供用户来使用,用户可以通过使用这些 API 完成对 GConf信息的管理。本文给出一个简单的示例程序 test.cpp。该程序的功能是获取当前的 FTP 协议的处理程序(对应的 command 值)。从程序的输出可以看出,笔者的操作系统对于 FTP 协议的默认启动程序是 firefox.


GConf 示例程序

                
#include <string.h>
#include <stdio.h>
#include <gconf/gconf-client.h>

int main (){

    //Get Gconf client
    GConfClient* gConfClient = gconf_client_get_default();
    if (!gConfClient) return 1 ;

    //Get the value
    gchar * str = gconf_client_get_string(gConfClient, 
"/desktop/gnome/url-handlers/ftp/command", NULL);

    if (str){
        printf("%s\n",str);
        //Free the value
        g_free(str);
    }

    //Unref this gobject
    g_object_unref(gConfClient);
    return 0;
}

 

编译 test.cpp:

cbz: ~/test$g++ `pkg-config --cflags --libs gconf-2.0` test.cpp

 

执行 test.cpp:

cbz: ~/test$./a.out
firefox %s

 

与命令行工具 gconftool-2 获得的结果对比:

cbz: ~/test$gconftool-2 -g /desktop/gnome/url-handlers/ftp/command
firefox %s


基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


结论

GConf 是基于 GNOME2 桌面的 Linux 操作系统中用于配置应用程序属性的管理工具。通过本文的讨论,读者可以获得对 GConf 有一个比较全面的了解,尤其对于在 Linux 操作系统中进行开发应用程序时,对于如何使用 GConf 对应用程序进行配置提供了一个参考以及对基于GConf 的 API 进行程序开发和设计进行了简单的介绍。

 

基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
基于 GNOME2 桌面的 GConf 配置系统的介绍
回页首


参考文献

[1] http://www.gnome.org/projects/gconf/

[2] http://developer.gnome.org/feature/archive/gconf/gconf.html

[3] http://library.gnome.org/devel/gconf/index.html

[4] http://library.gnome.org/admin/system-admin-guide/stable/