Gconf系统配置的介绍。

来源:互联网 发布:数据分析培训cda 编辑:程序博客网 时间:2024/04/30 16:04
 

昨天在看代码的时候,遇到了gconf这个陌生的对象,陌生只是相对我而言。然后花了一天的时间对它进行研究,从中学了不少的东西,在这里做一个笔记,好记性不如烂笔头阿。
      
       对于gconf的资料,网上一搜索都有很多,对于一些具体的情况,我就不做详细的介绍。简单的总结一下,gconf就是相当于windows下的注册表,可是它比注册表灵活很多。你可以通过设置它的KEY/VALUE值,然后很灵活的调用相关的应用程序。一个KEY可以跟踪多个回调函数,一旦它的属性值被改变,这些回调函数将会马上采取行动,使相关应用程序获得最新的信息,根据属性值作出相应的处理。比如手机主题的更改,它就很具体地体现出来了。

       gconf还有一个编辑工具,那就是gconftool-2,在终端,你可以通过它来设置gconf的值、列出一些gconf的属性值等操作。相关的操作可以通过资料查询或者通过man gconftool-2.
  这里我将通过代码(main.c)来简单的介绍gconf的实现过程。
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <gconf/gconf-client.h>


  4. #define  SET_KEY   "apps/theme_manager/theme"
  5. GMainloop* loop;


  6. static void 
  7. callback_func(GConfClient* gConfClient, guint cnxn_id, 
  8.                         GConfEntry* entry, gpointer user_data)
  9. {
  10.     gchar* strvalue = NULL;

  11.     if(strcmp(entry->key, SET_KEY) == 0)
  12.     {
  13.         strvalue = strdup(gconf_value_get_string(entry->value));
  14.         printf("the gconf changed: %s/n", strvalue);                        //打印出更新后的value.
  15.     }
  16.     else
  17.     {
  18.         printf("the gconf don't changed./n");
  19.     }
  20.     g_main_loop_quit(loop);

  21.     return ;
  22. }

  23. static void 
  24. time_callback_func(gpointer arg)
  25. {
  26.     g_main_loop_quit(loop);

  27.     return ;
  28. }


  29. int main(int argc, char**argv)
  30. {
  31.     char address[260] = {0};
  32.     GError* error = NULL;
  33.     g_type_init();

  34.     if(g_thread_supported() == 0)
  35.     {
  36.         g_thread_init(NULL);
  37.     }

  38.     loop = g_main_loop_new(NULL, FALSE);
  39.     if(loop == NULL)
  40.     {
  41.         g_error("Faile to create mainloop./n");
  42.     }
  43.     snprintf(address, sizeof(address),                                                                                                                     
  44.                   "xml:readwrite:%s/.gconf",
  45.                     g_get_home_dir());

  46.     GConfEngine* engine = gconf_engine_get_for_address (address, &error);          //初始化配置源
  47.     if(engine == NULL)
  48.     {
  49.         g_error("Faile to create engine./n");
  50.      }

  51.     GConfClient*  gConfClient = gconf_client_get_for_engine (engine);
  52.     gboolean ret = gconf_client_set_string(gConfClient, 
  53.                                                                    SET_KEY, 
  54.                                                                    "broncho",
  55.                                                                         NULL);                  //设置key的属性值

  56.     if(ret)
  57.     {
  58.         char* str = gconf_client_get_string(gConfClient, 
  59.                                                                   SET_KEY,
  60.                                                                    NULL);                       //得到key的属性值
  61.         if(str != NULL)
  62.         {
  63.             printf("the str = %s /n",str);
  64.         }
  65.     }
  66.     gchar* dir = strdup(SET_KEY);
  67.     gchar* p = strrchr(dir, '/');
  68.     if(p != NULL)*p = '/0';

  69.     gconf_client_add_dir(gConfClient, dir, 
  70.                                         GCONF_CLIENT_PRELOAD_NONE,
  71.                                         NULL);                                                    //设置监视路径
  72.     g_free(dir);
  73.     gconf_client_notify_add(gConfClient, SET_KEY , 
  74.                                             callback_func, NULL, NULL,      
  75.                                              NULL);                                               //注册key通知机制

  76.     g_timeout_add(10000, time_callback_func, NULL);                   //时间设置
  77.     g_main_loop_run(loop);
  78.     g_main_loop_unref(loop);
  79.     g_object_unref(gConfClient);
  80.     return 0;

  81. }
然后编译该文件:
  1. gcc  -g `pkg-config  --cflags   --libs  glib-2.0  gthread-2.0   gconf-2.0`  main.c -o test 
编译了之后,运行可执行文件:  ./test ,终端将会输出:
  1. the str = broncho 
然后在十秒钟之内在另外一个终端输入下面指令:
  1. gconftool-2 -type  string  -s  /apps/theme_manager/theme   helloworld
这时候gconf的属性值将产生改变,这时候将调用回调函数,打印出更新后的属性值,退出main_loop:
  1. the str = broncho 
  2. the gconf changed: helloworld


  这就是gconf的整个实现过程,你可以通过“主目录/.gconf/apps/theme_manager”下的%gconf.xml查看相关源文件的。