-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnodegsettings.cc
406 lines (348 loc) · 12.1 KB
/
nodegsettings.cc
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* GPII Node.js GSettings Bridge
*
* Copyright 2012 Steven Githens
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*
*/
/*
* For changes done after initial fork from
* https://github.com/GPII/linux
* based on commit b28e99212e4c7cc803f210c066e0124d72c8b72d.
*
* Copyright 2015 Leopold Burdyl
*/
#include <node.h>
#include <v8.h>
#include <gio/gio.h>
using namespace v8;
// globals
const GVariantType* X_G_VARIANT_TYPE_STRING_TUPLE_ARRAY = g_variant_type_new("a(ss)");
/*
Helper function - because we do this A LOT
The returned char needs to be freed after usage.
Use 'g_free' to do so
*/
char * v8_value_to_utf8_string(const Local<Value> value_obj) {
Local<String> string_obj = value_obj->ToString();
int utf8_string_length = string_obj->Utf8Length();
char *utf8_string = (char*) g_malloc(utf8_string_length + 1);
string_obj->WriteUtf8(utf8_string);
return utf8_string;
}
/* Takes schema_id string */
Handle<Value> get_gsetting_keys(const Arguments& args) {
HandleScope scope;
GSettings *settings;
gchar ** keys;
gint i;
gint size = 0;
char *schema_id = v8_value_to_utf8_string(args[0]);
settings = g_settings_new(schema_id);
g_free((void *) schema_id);
keys = g_settings_list_keys(settings);
size = sizeof(keys)/sizeof(keys[0]);
Handle<Array> togo;
togo = Array::New(size);
for (i = 0; keys[i]; i++) {
togo->Set(i,String::New(keys[i]));
}
g_strfreev(keys);
return scope.Close(togo);
}
/* Takes schema_id and key */
Handle<Value> get_gsetting(const Arguments& args) {
HandleScope scope;
GSettings *settings;
GVariant *variant;
const GVariantType *type;
const char *schema_id;
const char *key;
schema_id = v8_value_to_utf8_string(args[0]);
key = v8_value_to_utf8_string(args[1]);
// validate key and schema
GSettingsSchemaSource *schema_source = g_settings_schema_source_get_default();
if ( ! schema_source ) {
ThrowException(Exception::Error(String::New("No schema source available!")));
g_free((void *) schema_id);
g_free((void *) key);
return scope.Close(Undefined());
}
GSettingsSchema * gsettings_schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
if ( ! schema_source ) {
printf("Schema '%s' is not installed!\n", schema_id);
ThrowException(Exception::Error(String::New("Schema is not installed!")));
g_free((void *) schema_id);
g_free((void *) key);
return scope.Close(Undefined());
}
gboolean has_key = g_settings_schema_has_key (gsettings_schema, key);
if ( ! has_key ) {
printf("Key '%s' does not exist!\n", key);
ThrowException(Exception::Error(String::New("Key does not exist!")));
g_free((void *) schema_id);
g_free((void *) key);
return scope.Close(Undefined());
}
// create varient
settings = g_settings_new(schema_id);
variant = g_settings_get_value(settings,key);
type = g_variant_get_type(variant);
g_free((void *) key);
g_free((void *) schema_id);
if (g_variant_type_equal(type,G_VARIANT_TYPE_DOUBLE)) {
return scope.Close(Number::New(g_variant_get_double(variant)));
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_INT32)) {
return scope.Close(Int32::New(g_variant_get_int32(variant)));
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_UINT32)) {
const guint gunit32_value = g_variant_get_uint32(variant);
/*
IMPORTANT:
Using 'Unit32::New(gunit32_value)''
will wrong results on some architectures
as the uint max limit can be different.
As I understand it, v8 will try to cast
the guint value to the architecture specific
uint value. If the max uint value is lower
than the one of guint, the value can be corrupted
and can (as I experienced it) be set to -1
To keep v8 from casting the guint
value to a architecture dependent lower
limited unit
we use the more general 'Number' class
that supports long values.
*/
return scope.Close(Number::New(gunit32_value));
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_STRING)) {
return scope.Close(String::New(g_variant_get_string(variant,NULL)));
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_STRING_ARRAY)) {
gsize length;
const gchar **elems = g_variant_get_strv(variant,&length);
HandleScope scope;
Local<Array> nodes = Array::New();
for (unsigned int i = 0; i < length; ++i) {
nodes->Set(i, String::New(elems[i]));
}
g_free(elems);
return scope.Close(nodes);
}
else if (g_variant_type_equal(type, X_G_VARIANT_TYPE_STRING_TUPLE_ARRAY)) {
Local<Array> tuple_array = Array::New();
GVariantIter *iter;
gchar *str1;
gchar *str2;
unsigned int i = 0;
g_variant_get (variant, "a(ss)", &iter);
while (g_variant_iter_loop (iter, "(ss)", &str1, &str2)) {
Local<Array> tuple = Array::New();
tuple->Set(0, String::New(str1));
tuple->Set(1, String::New(str2));
tuple_array->Set(i, tuple);
i++;
}
g_variant_iter_free (iter);
return scope.Close(tuple_array);
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_BOOLEAN)) {
return scope.Close(Boolean::New(g_variant_get_boolean(variant)));
}
else {
g_print("The type is %s\n", g_variant_type_peek_string(type));
ThrowException(Exception::Error(String::New("Need to implement reading that value type")));
return scope.Close(Undefined());
}
}
/* Takes schema_id string */
Handle<Value> schema_exists(const Arguments& args) {
HandleScope scope;
char *schema_id = v8_value_to_utf8_string(args[0]);
GSettingsSchemaSource *schema_source = g_settings_schema_source_get_default();
if ( ! schema_source ) {
g_free(schema_id);
ThrowException(Exception::Error(String::New("No schema sources available!")));
return scope.Close(Undefined());
}
GSettingsSchema * schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
g_free(schema_id);
if ( ! schema ) {
return scope.Close(Boolean::New(FALSE));
}
return scope.Close(Boolean::New(TRUE));
}
/* Takes schema_id, key, and value */
Handle<Value> set_gsetting(const Arguments& args) {
HandleScope scope;
Local<Value> value_obj = args[2];
GSettings *settings;
GVariant *variant;
GVariant *variant_to_set;
const char *validation_fail_message;
const GVariantType *type;
bool write_success = false;
bool validation_success = false;
const char *schema_id;
const char *key;
schema_id = v8_value_to_utf8_string(args[0]);
key = v8_value_to_utf8_string(args[1]);
// validate key and schema
GSettingsSchemaSource *schema_source = g_settings_schema_source_get_default();
if ( ! schema_source ) {
g_free((void *) schema_id);
g_free((void *) key);
ThrowException(Exception::Error(String::New("No schema source available!")));
return scope.Close(Undefined());
}
GSettingsSchema * gsettings_schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
if ( ! schema_source ) {
printf("Schema '%s' is not installed!\n", schema_id);
g_free((void *) schema_id);
g_free((void *) key);
ThrowException(Exception::Error(String::New("Schema is not installed!")));
return scope.Close(Undefined());
}
gboolean has_key = g_settings_schema_has_key (gsettings_schema, key);
if ( ! has_key ) {
printf("Key '%s' does not exist!\n", key);
g_free((void *) schema_id);
g_free((void *) key);
ThrowException(Exception::Error(String::New("Key does not exist!")));
return scope.Close(Undefined());
}
GSettingsSchemaKey * gsettings_key =
g_settings_schema_get_key (gsettings_schema, key);
// get required variant type
settings = g_settings_new(schema_id);
variant = g_settings_get_value(settings,key);
type = g_variant_get_type(variant);
g_free((void *) schema_id);
if ( g_variant_type_equal(type,G_VARIANT_TYPE_BOOLEAN) ) {
if ( value_obj->IsBoolean() ) {
validation_success = true;
variant_to_set = g_variant_new_boolean(value_obj->BooleanValue());
} else {
validation_fail_message = "Key requires boolean value!";
}
}
else if ( g_variant_type_equal(type,G_VARIANT_TYPE_STRING) ) {
if ( value_obj->IsString() ) {
validation_success = true;
char *val = v8_value_to_utf8_string(value_obj);
variant_to_set = g_variant_new_string((const gchar *) val);
g_free( (void *) val );
} else {
validation_fail_message = "Key requires string value!";
}
}
else if ( g_variant_type_equal(type, G_VARIANT_TYPE_DOUBLE) ) {
if ( value_obj->IsNumber() ) {
validation_success = true;
variant_to_set = g_variant_new_double(value_obj->ToNumber()->Value());
} else {
validation_fail_message = "Key requires a number!";
}
}
else if ( g_variant_type_equal(type, G_VARIANT_TYPE_INT32) ) {
if ( value_obj->IsInt32() ) {
validation_success = true;
variant_to_set = g_variant_new_int32 (value_obj->ToInt32()->Value());
} else {
validation_fail_message = "Key requires a integer number!";
}
}
else if ( g_variant_type_equal(type, G_VARIANT_TYPE_UINT32) ) {
if ( value_obj->IsUint32() ) {
validation_success = true;
variant_to_set = g_variant_new_uint32(value_obj->ToUint32()->Value());
} else {
validation_fail_message = "Key requires a unsigned integer number!";
}
}
else if ( g_variant_type_equal(type, G_VARIANT_TYPE_STRING_ARRAY) ) {
if ( value_obj->IsArray() ) {
validation_success = true;
GVariantBuilder builder;
uint i;
uint length;
Local<Object> obj = value_obj->ToObject();
// get the array length
length = obj->Get(String::New("length"))->ToObject()->Uint32Value();
g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
for(i = 0; i < length; i++)
{
Local<Value> element = obj->Get(i);
if ( element->IsString() ) {
char *val = v8_value_to_utf8_string(element);
GVariant *string_variant = g_variant_new_string ((const gchar *) val);
g_free((void *) val);
g_variant_builder_add_value (&builder, string_variant);
}
else {
g_free((void *) key);
ThrowException(Exception::Error(String::New("Array item have to be strings!")));
return scope.Close(Undefined());
}
}
variant_to_set = g_variant_builder_end (&builder);
} else {
validation_fail_message = "Key requires an array of strings!";
}
}
else {
validation_fail_message = "We haven't implemented this type yet!";
}
if ( ! validation_success ) {
g_free((void *) key);
ThrowException(Exception::Error(String::New(validation_fail_message)));
return scope.Close(Undefined());
}
// write variant
gboolean is_valid = g_settings_schema_key_range_check (gsettings_key, variant_to_set);
if ( ! is_valid ) {
printf("Invalid range or type of '%s'\n", key);
g_free((void *) key);
ThrowException(Exception::Error(String::New("Invalid range or type!")));
return scope.Close(Undefined());
}
write_success = g_settings_set_value(settings, key, variant_to_set);
g_settings_sync();
g_free((void *) key);
if ( ! write_success ) {
ThrowException(Exception::Error(String::New("Failed to set gsetting! Key is write protected.")));
}
return scope.Close(Undefined());
}
/* in addon initialization function */
void init(Handle<Object> target) {
setvbuf(stdout, NULL, _IONBF, 0);
target->Set(String::NewSymbol("set_gsetting"),
FunctionTemplate::New(set_gsetting)->GetFunction());
target->Set(String::NewSymbol("get_gsetting"),
FunctionTemplate::New(get_gsetting)->GetFunction());
target->Set(String::NewSymbol("get_gsetting_keys"),
FunctionTemplate::New(get_gsetting_keys)->GetFunction());
target->Set(String::NewSymbol("schema_exists"),
FunctionTemplate::New(schema_exists)->GetFunction());
}
NODE_MODULE(nodegsettings, init)