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的实现过程。
- #include <stdlib.h>
- #include <stdio.h>
- #include <gconf/gconf-client.h>
- #define SET_KEY "apps/theme_manager/theme"
- GMainloop* loop;
- static void
- callback_func(GConfClient* gConfClient, guint cnxn_id,
- GConfEntry* entry, gpointer user_data)
- {
- gchar* strvalue = NULL;
- if(strcmp(entry->key, SET_KEY) == 0)
- {
- strvalue = strdup(gconf_value_get_string(entry->value));
- printf("the gconf changed: %s/n", strvalue); //打印出更新后的value.
- }
- else
- {
- printf("the gconf don't changed./n");
- }
- g_main_loop_quit(loop);
- return ;
- }
- static void
- time_callback_func(gpointer arg)
- {
- g_main_loop_quit(loop);
- return ;
- }
- int main(int argc, char**argv)
- {
- char address[260] = {0};
- GError* error = NULL;
- g_type_init();
- if(g_thread_supported() == 0)
- {
- g_thread_init(NULL);
- }
- loop = g_main_loop_new(NULL, FALSE);
- if(loop == NULL)
- {
- g_error("Faile to create mainloop./n");
- }
- snprintf(address, sizeof(address),
- "xml:readwrite:%s/.gconf",
- g_get_home_dir());
- GConfEngine* engine = gconf_engine_get_for_address (address, &error); //初始化配置源
- if(engine == NULL)
- {
- g_error("Faile to create engine./n");
- }
- GConfClient* gConfClient = gconf_client_get_for_engine (engine);
- gboolean ret = gconf_client_set_string(gConfClient,
- SET_KEY,
- "broncho",
- NULL); //设置key的属性值
- if(ret)
- {
- char* str = gconf_client_get_string(gConfClient,
- SET_KEY,
- NULL); //得到key的属性值
- if(str != NULL)
- {
- printf("the str = %s /n",str);
- }
- }
- gchar* dir = strdup(SET_KEY);
- gchar* p = strrchr(dir, '/');
- if(p != NULL)*p = '/0';
- gconf_client_add_dir(gConfClient, dir,
- GCONF_CLIENT_PRELOAD_NONE,
- NULL); //设置监视路径
- g_free(dir);
- gconf_client_notify_add(gConfClient, SET_KEY ,
- callback_func, NULL, NULL,
- NULL); //注册key通知机制
- g_timeout_add(10000, time_callback_func, NULL); //时间设置
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
- g_object_unref(gConfClient);
- return 0;
- }
然后编译该文件:
- gcc -g `pkg-config --cflags --libs glib-2.0 gthread-2.0 gconf-2.0` main.c -o test
编译了之后,运行可执行文件: ./test ,终端将会输出:
- the str = broncho
然后在十秒钟之内在另外一个终端输入下面指令:
- gconftool-2 -type string -s /apps/theme_manager/theme helloworld
这时候gconf的属性值将产生改变,这时候将调用回调函数,打印出更新后的属性值,退出main_loop:
- the str = broncho
- the gconf changed: helloworld
这就是gconf的整个实现过程,你可以通过“主目录/.gconf/apps/theme_manager”下的%gconf.xml查看相关源文件的。
- #include <stdlib.h>
- #include <stdio.h>
- #include <gconf/gconf-client.h>
- #define SET_KEY "apps/theme_manager/theme"
- GMainloop* loop;
- static void
- callback_func(GConfClient* gConfClient, guint cnxn_id,
- GConfEntry* entry, gpointer user_data)
- {
- gchar* strvalue = NULL;
- if(strcmp(entry->key, SET_KEY) == 0)
- {
- strvalue = strdup(gconf_value_get_string(entry->value));
- printf("the gconf changed: %s/n", strvalue); //打印出更新后的value.
- }
- else
- {
- printf("the gconf don't changed./n");
- }
- g_main_loop_quit(loop);
- return ;
- }
- static void
- time_callback_func(gpointer arg)
- {
- g_main_loop_quit(loop);
- return ;
- }
- int main(int argc, char**argv)
- {
- char address[260] = {0};
- GError* error = NULL;
- g_type_init();
- if(g_thread_supported() == 0)
- {
- g_thread_init(NULL);
- }
- loop = g_main_loop_new(NULL, FALSE);
- if(loop == NULL)
- {
- g_error("Faile to create mainloop./n");
- }
- snprintf(address, sizeof(address),
- "xml:readwrite:%s/.gconf",
- g_get_home_dir());
- GConfEngine* engine = gconf_engine_get_for_address (address, &error); //初始化配置源
- if(engine == NULL)
- {
- g_error("Faile to create engine./n");
- }
- GConfClient* gConfClient = gconf_client_get_for_engine (engine);
- gboolean ret = gconf_client_set_string(gConfClient,
- SET_KEY,
- "broncho",
- NULL); //设置key的属性值
- if(ret)
- {
- char* str = gconf_client_get_string(gConfClient,
- SET_KEY,
- NULL); //得到key的属性值
- if(str != NULL)
- {
- printf("the str = %s /n",str);
- }
- }
- gchar* dir = strdup(SET_KEY);
- gchar* p = strrchr(dir, '/');
- if(p != NULL)*p = '/0';
- gconf_client_add_dir(gConfClient, dir,
- GCONF_CLIENT_PRELOAD_NONE,
- NULL); //设置监视路径
- g_free(dir);
- gconf_client_notify_add(gConfClient, SET_KEY ,
- callback_func, NULL, NULL,
- NULL); //注册key通知机制
- g_timeout_add(10000, time_callback_func, NULL); //时间设置
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
- g_object_unref(gConfClient);
- return 0;
- }
- gcc -g `pkg-config --cflags --libs glib-2.0 gthread-2.0 gconf-2.0` main.c -o test
- the str = broncho
- gconftool-2 -type string -s /apps/theme_manager/theme helloworld
- the str = broncho
- the gconf changed: helloworld
- Gconf系统配置的介绍。
- Gconf系统配置的介绍。
- 基于 GNOME2 桌面的 GConf 配置系统的介绍
- gconf-editor的使用
- 关于Gconf改造的构想
- gconf-editor,Ubuntu的注册表
- gconf-editor相关的内容
- Gsettings and GConf 介绍、侧重点比较
- 系统配置文件介绍
- python rodeo遇到的GConf Error问题
- linux 系统配置文件分类介绍
- Debian的系统配置
- ubuntu 的系统配置文件
- SystemParametersinfo系统配置的获取
- 我的linux系统配置
- linux 善用 gconf-editor 工具(linux的注册表工具)
- 系统配置
- 系统配置
- 『JSP』jsp 标签taglib不起作用的问题
- InsertSortFun
- Hibernate多对多举例
- SqlServer插入慢的问题解决
- 资料收集备用
- Gconf系统配置的介绍。
- error: unknown field ‘ioctl’ specified in initializer
- android中Mms学习笔记
- ShellSortFun
- jQuery hover 做tooltip工具提示效果
- 如何向android的setting语言列表中添加一门语言
- 汇丰再裁150人IT部门成重灾 拒绝评论裁员消息
- 数据库三级模式结构
- 数据广播和subtitle、teletext、close caption之间的关系