71d3361ce26cfd5d03266cb10e09bada79dc50dd
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / reg_backend_gconf / reg_backend_gconf.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij                                          2004.
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/common/registry.h"
23 #include <gconf/gconf-client.h>
24
25 static BOOL reg_open_gconf(REG_HANDLE *h, const char *location, BOOL try_complete_load) 
26 {
27         h->backend_data = (void *)gconf_client_get_default();
28         return True;
29 }
30
31 static BOOL reg_close_gconf(REG_HANDLE *h) 
32 {
33         return True;
34 }
35
36 static REG_KEY *gconf_open_key (REG_HANDLE *h, const char *name) 
37 {
38         REG_KEY *ret;
39         char *fullpath = reg_path_win2unix(strdup(name));
40
41         /* Check if key exists */
42         if(!gconf_client_dir_exists((GConfClient *)h->backend_data, fullpath, NULL)) {
43                 SAFE_FREE(fullpath);
44                 return NULL;
45         }
46         ret = reg_key_new_abs(name, h, NULL);
47         ret->backend_data = talloc_strdup(ret->mem_ctx, fullpath);
48         SAFE_FREE(fullpath);
49
50         return ret;
51 }
52
53 static BOOL gconf_fetch_values(REG_KEY *p, int *count, REG_VAL ***vals)
54 {
55         GSList *entries;
56         GSList *cur;
57         REG_VAL **ar = talloc(p->mem_ctx, sizeof(REG_VAL *));
58         char *fullpath = p->backend_data;
59         cur = entries = gconf_client_all_entries((GConfClient*)p->handle->backend_data, fullpath, NULL);
60
61         (*count) = 0;
62         while(cur) {
63                 GConfEntry *entry = cur->data;
64                 GConfValue *value = gconf_entry_get_value(entry);
65                 REG_VAL *newval = reg_val_new(p, NULL);
66                 newval->name = talloc_strdup(newval->mem_ctx, strrchr(gconf_entry_get_key(entry), '/')+1);
67                 if(value) {
68                         switch(value->type) {
69                         case GCONF_VALUE_INVALID: 
70                                 newval->data_type = REG_NONE;
71                                 break;
72
73                         case GCONF_VALUE_STRING:
74                                 newval->data_type = REG_SZ;
75                                 newval->data_blk = talloc_strdup(newval->mem_ctx, gconf_value_get_string(value));
76                                 newval->data_len = strlen(newval->data_blk);
77                                 break;
78
79                         case GCONF_VALUE_INT:
80                                 newval->data_type = REG_DWORD;
81                                 newval->data_blk = talloc(newval->mem_ctx, sizeof(long));
82                                 *((long *)newval->data_blk) = gconf_value_get_int(value);
83                                 newval->data_len = sizeof(long);
84                                 break;
85
86                         case GCONF_VALUE_FLOAT:
87                                 newval->data_blk = talloc(newval->mem_ctx, sizeof(double));
88                                 newval->data_type = REG_BINARY;
89                                 *((double *)newval->data_blk) = gconf_value_get_float(value);
90                                 newval->data_len = sizeof(double);
91                                 break;
92
93                         case GCONF_VALUE_BOOL:
94                                 newval->data_blk = talloc(newval->mem_ctx, sizeof(BOOL));
95                                 newval->data_type = REG_BINARY;
96                                 *((BOOL *)newval->data_blk) = gconf_value_get_bool(value);
97                                 newval->data_len = sizeof(BOOL);
98                                 break;
99
100                         default:
101                                 newval->data_type = REG_NONE;
102                                 DEBUG(0, ("Not implemented..\n"));
103                                 break;
104                         }
105                 } else newval->data_type = REG_NONE; 
106
107                 ar[(*count)] = newval;
108                 ar = talloc_realloc(p->mem_ctx, ar, sizeof(REG_VAL *) * ((*count)+2));
109                 (*count)++;
110                 g_free(cur->data);
111                 cur = cur->next;
112         }
113
114         g_slist_free(entries);
115         *vals = ar;
116         return True;
117 }
118
119 static BOOL gconf_fetch_subkeys(REG_KEY *p, int *count, REG_KEY ***subs) 
120 {
121         GSList *dirs;
122         GSList *cur;
123         REG_KEY **ar = malloc(sizeof(REG_KEY *));
124         char *fullpath = p->backend_data;
125         cur = dirs = gconf_client_all_dirs((GConfClient*)p->handle->backend_data, fullpath,NULL);
126
127         (*count) = 0;
128         while(cur) {
129                 ar[(*count)] = reg_key_new_abs(reg_path_unix2win((char *)cur->data), p->handle,NULL);
130                 ar[(*count)]->backend_data = talloc_strdup(ar[*count]->mem_ctx, cur->data);
131                 ar = realloc(ar, sizeof(REG_KEY *) * ((*count)+2));
132                 (*count)++;
133                 g_free(cur->data);
134                 cur = cur->next;
135         }
136
137         g_slist_free(dirs);
138         *subs = ar;
139         return True;
140 }
141
142 static BOOL gconf_update_value(REG_VAL *val, int type, void *data, int len)
143 {
144         GError *error = NULL;
145         char *keypath = val->backend_data;
146         char *valpath;
147         if(val->name)asprintf(&valpath, "%s/%s", keypath, val->name);
148         else valpath = strdup(keypath);
149         
150         switch(type) {
151         case REG_SZ:
152         case REG_EXPAND_SZ:
153                 gconf_client_set_string((GConfClient *)val->handle->backend_data, valpath, data, &error);
154                 free(valpath);
155                 return (error == NULL);
156
157         case REG_DWORD:
158                 gconf_client_set_int((GConfClient *)val->handle->backend_data, valpath, 
159  *((int *)data), &error);
160                 free(valpath);
161                 return (error == NULL);
162         default:
163                 DEBUG(0, ("Unsupported type: %d\n", type));
164                 free(valpath);
165                 return False;
166         }
167         return False;
168 }
169
170 static REG_OPS reg_backend_gconf = {
171         .name = "gconf",
172         .open_registry = reg_open_gconf,
173         .close_registry = reg_close_gconf,
174         .open_key = gconf_open_key,
175         .fetch_subkeys = gconf_fetch_subkeys,
176         .fetch_values = gconf_fetch_values,
177         .update_value = gconf_update_value,
178         
179         /* Note: 
180          * since GConf uses schemas for what keys and values are allowed, there 
181          * is no way of 'emulating' add_key and del_key here.
182          */
183 };
184
185 NTSTATUS reg_gconf_init(void)
186 {
187         return register_backend("registry", &reg_backend_gconf);
188 }