This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathxfconf.c
258 lines (245 loc) · 9.02 KB
/
xfconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @file
*
* @brief Source for xfconf plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "xfconf.h"
#include "kdberrors.h"
#include <kdb.h>
#include <kdbease.h>
#include <kdbhelper.h>
#include <kdblogger.h>
#include <xfconf/xfconf.h>
int elektraXfconfInit (Key * errorKey, int xfconfCode, int xfconfShutdown)
{
ELEKTRA_LOG ("try to initialize xfconf\n");
GError * err = NULL;
if (xfconf_init (&err))
{
ELEKTRA_LOG_DEBUG ("succeed initialize xfconf\n");
if (xfconfShutdown)
{
xfconf_shutdown ();
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
else
{
ELEKTRA_LOG ("unable to initialize xfconf(%d): %s\n", err->code, err->message);
int status = ELEKTRA_PLUGIN_STATUS_ERROR;
ELEKTRA_SET_INTERFACE_ERROR (errorKey, err->message);
if (xfconfCode)
{
status = err->code;
}
g_error_free (err);
if (xfconfShutdown)
{
xfconf_shutdown ();
}
return status;
}
}
int elektraXfconfOpen (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
return elektraXfconfInit (errorKey, 0, 0);
}
int elektraXfconfClose (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
xfconf_shutdown ();
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
static void appendChannelList (KeySet * keySet)
{
const char * absoluteKeyName = "system:/elektra/modules/xfconf/channels";
Key * channelArrayKey = keyNew (absoluteKeyName, KEY_END);
gchar ** channels = xfconf_list_channels ();
if (channels == NULL)
{
return;
}
keySetMeta (channelArrayKey, "array", "");
long channelIndex;
for (channelIndex = 0; channels[channelIndex] != NULL; channelIndex++)
{
char * channelKeyName = elektraMalloc ((elektraStrLen (absoluteKeyName) + ELEKTRA_MAX_ARRAY_SIZE + 2) * sizeof (char));
channelKeyName[0] = '\0';
strcat (channelKeyName, absoluteKeyName);
channelKeyName[elektraStrLen (absoluteKeyName) - 1] = '/';
elektraWriteArrayNumber (&channelKeyName[elektraStrLen (absoluteKeyName)], channelIndex);
Key * currentChannelKey = keyNew (channelKeyName, KEY_VALUE, channels[channelIndex], KEY_END);
ksAppendKey (keySet, currentChannelKey);
}
if (channelIndex > 0)
{
char * arrayValue = elektraMalloc (ELEKTRA_MAX_ARRAY_SIZE * sizeof (char));
elektraWriteArrayNumber (arrayValue, channelIndex - 1);
keySetMeta (channelArrayKey, "array", arrayValue);
}
ksAppendKey (keySet, channelArrayKey);
g_strfreev (channels);
}
int elektraXfconfGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
ELEKTRA_LOG ("issued get\n");
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/xfconf"))
{
ELEKTRA_LOG_DEBUG ("getting system modules values\n");
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/xfconf", KEY_VALUE, "xfconf plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/xfconf/exports", KEY_END),
keyNew ("system:/elektra/modules/xfconf/exports/open", KEY_FUNC, elektraXfconfOpen, KEY_END),
keyNew ("system:/elektra/modules/xfconf/exports/close", KEY_FUNC, elektraXfconfClose, KEY_END),
keyNew ("system:/elektra/modules/xfconf/exports/get", KEY_FUNC, elektraXfconfGet, KEY_END),
keyNew ("system:/elektra/modules/xfconf/exports/set", KEY_FUNC, elektraXfconfSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/xfconf/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
appendChannelList (returned);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
// get all keys
KeySet * config = elektraPluginGetConfig (handle);
const Key * channelKey = ksLookupByName (config, "/channel", KDB_O_NONE);
const char * channelName = keyString (channelKey);
ELEKTRA_LOG_DEBUG ("fetch keys from channel: %s\n", channelName);
XfconfChannel * channel = xfconf_channel_get (channelName);
if (channel == NULL)
{
ELEKTRA_LOG_DEBUG ("retrieved NULL attempting getting channel: %s\n", channelName);
}
GHashTable * properties = xfconf_channel_get_properties (channel, NULL);
if (properties == NULL)
{
ELEKTRA_LOG_DEBUG ("retrieved NULL attempting getting properties\n");
}
GList * channelKeys = g_hash_table_get_keys (properties);
const char * parentName = keyName (parentKey);
while (channelKeys != NULL)
{
char * keyName = elektraStrDup (channelKeys->data);
char * absoluteKeyName = elektraMalloc ((elektraStrLen (keyName) + elektraStrLen (parentName)) * sizeof (char));
absoluteKeyName[0] = '\0';
strcat (absoluteKeyName, parentName);
strcat (absoluteKeyName, keyName);
Key * key = keyNew (absoluteKeyName, KEY_END);
GPtrArray * array = xfconf_channel_get_arrayv (channel, keyName);
if (array != NULL)
{
ELEKTRA_LOG_DEBUG ("found non-null array with size %d\n", array->len);
keySetMeta (key, "array", "");
for (guint i = 0; i < array->len; i++)
{
GValue * val = g_ptr_array_index (array, i);
char * arrayKeyName =
elektraMalloc ((elektraStrLen (absoluteKeyName) + ELEKTRA_MAX_ARRAY_SIZE + 2) * sizeof (char));
arrayKeyName[0] = '\0';
strcat (arrayKeyName, absoluteKeyName);
arrayKeyName[elektraStrLen (absoluteKeyName) - 1] = '/';
elektraWriteArrayNumber (&arrayKeyName[elektraStrLen (absoluteKeyName)], i);
Key * arrayKey = keyNew (arrayKeyName, KEY_END);
const gchar * arrayKeyValue = g_value_get_string (val);
ELEKTRA_LOG_DEBUG ("write to array key %s -> %s\n", arrayKeyName, arrayKeyValue);
keySetString (arrayKey, arrayKeyValue); // todo: care about data types
ksAppendKey (returned, arrayKey);
}
}
else
{
GValue keyValue = G_VALUE_INIT;
g_value_init (&keyValue, G_TYPE_STRING);
xfconf_channel_get_property (channel, keyName, &keyValue);
keySetString (key, g_value_get_string (&keyValue)); // todo: care about data types
ELEKTRA_LOG_DEBUG ("found %s -> %s\n", keyName, g_value_get_string (&keyValue));
}
if (xfconf_channel_is_property_locked (channel, keyName))
{
keyLock (key, KEY_LOCK_META | KEY_LOCK_NAME | KEY_LOCK_VALUE);
}
ksAppendKey (returned, key);
channelKeys = channelKeys->next;
}
g_list_free (channelKeys);
g_hash_table_destroy (properties);
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
int elektraXfconfSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
ELEKTRA_LOG_DEBUG ("issued set with parent %s\n", keyName (parentKey));
KeySet * config = elektraPluginGetConfig (handle);
const Key * channelKey = ksLookupByName (config, "/channel", KDB_O_NONE);
const char * channelName = keyString (channelKey);
ELEKTRA_LOG_DEBUG ("using channel %s of parent %s\n", channelName, keyName (parentKey));
XfconfChannel * channel = xfconf_channel_get (channelName);
if (channel == NULL)
{
ELEKTRA_LOG_DEBUG ("retrieved NULL attempting getting channel: %s\n", channelName);
}
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
Key * cur = ksNext (returned);
const char * currentKeyName = elektraKeyGetRelativeName (cur, parentKey);
if (currentKeyName == NULL)
{
// happens for the root key which holds the channel name
ELEKTRA_LOG_DEBUG ("currentKeyName is null!\n");
continue;
}
char * xfconfKeyName = elektraMalloc ((elektraStrLen (currentKeyName) + 2) * sizeof (char *));
xfconfKeyName[0] = '/';
strncpy (&xfconfKeyName[1], currentKeyName, elektraStrLen (currentKeyName));
ELEKTRA_LOG_DEBUG ("setting key %s to %s\n", xfconfKeyName, keyString (cur));
if (keyGetMeta (cur, "array") != NULL)
{
ELEKTRA_LOG_DEBUG ("key %s is an array\n", xfconfKeyName);
KeySet * arrayKeySet = elektraArrayGet (cur, returned);
Key * arrayKey;
GPtrArray * xfconfArray = g_ptr_array_new ();
while ((arrayKey = ksNext (arrayKeySet)) != NULL)
{
gchar * itemValue = elektraStrDup (keyString (arrayKey));
keyDel (arrayKey);
ELEKTRA_LOG_DEBUG ("found array value %s\n", itemValue);
GValue keyValue = G_VALUE_INIT;
g_value_init (&keyValue, G_TYPE_STRING);
g_value_set_string (&keyValue, itemValue);
g_ptr_array_add (xfconfArray, &keyValue); // todo: determine item types, they throw critical errors
}
if (!xfconf_channel_set_arrayv (channel, xfconfKeyName, xfconfArray))
{
ELEKTRA_LOG_DEBUG ("unable to set array value\n");
}
}
else
{
ELEKTRA_LOG_DEBUG ("key %s is a single value\n", xfconfKeyName);
GValue keyValue = G_VALUE_INIT;
if (!xfconf_channel_get_property (channel, xfconfKeyName, &keyValue))
{
ELEKTRA_LOG_DEBUG ("key was not found, initialize a new one of type string\n");
g_value_init (&keyValue, G_TYPE_STRING);
}
ELEKTRA_LOG_DEBUG ("key is of type: %lu\n", keyValue.g_type);
g_value_set_string (&keyValue, keyString (cur));
if (!xfconf_channel_set_property (channel, xfconfKeyName, &keyValue))
{
ELEKTRA_LOG_DEBUG ("unable to set value\n");
}
}
}
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("xfconf",
ELEKTRA_PLUGIN_OPEN, &elektraXfconfOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraXfconfClose,
ELEKTRA_PLUGIN_GET, &elektraXfconfGet,
ELEKTRA_PLUGIN_SET, &elektraXfconfSet,
ELEKTRA_PLUGIN_END);
}