forked from gcampax/node-gsettings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodegsettings.cc
More file actions
417 lines (361 loc) · 14.1 KB
/
nodegsettings.cc
File metadata and controls
417 lines (361 loc) · 14.1 KB
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
407
408
409
410
411
412
413
414
415
416
417
/*
* 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 */
void get_gsetting_keys(const FunctionCallbackInfo<Value>& args) {
GSettings *settings;
gchar ** keys;
gint i;
gint size = 0;
Isolate *isolate = args.GetIsolate();
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]);
Local<Array> togo;
togo = Array::New(isolate, size);
for (i = 0; keys[i]; i++) {
togo->Set(i, String::NewFromOneByte(isolate, (const uint8_t*) keys[i], NewStringType::kNormal).ToLocalChecked() );
}
g_strfreev(keys);
args.GetReturnValue().Set(togo);
}
/* Takes schema_id and key */
void get_gsetting(const FunctionCallbackInfo<Value>& args) {
GSettings *settings;
GVariant *variant;
const GVariantType *type;
const char *schema_id;
const char *key;
Isolate *isolate = args.GetIsolate();
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 ) {
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "No schema source available!", NewStringType::kNormal ).ToLocalChecked() ));
g_free((void *) schema_id);
g_free((void *) key);
args.GetReturnValue().SetUndefined();
return;
}
GSettingsSchema * gsettings_schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
if ( ! schema_source ) {
isolate->ThrowException(Exception::Error(String::NewFromOneByte(isolate, (const uint8_t*) "Schema is not installed!", NewStringType::kNormal ).ToLocalChecked() ));
g_free((void *) schema_id);
g_free((void *) key);
args.GetReturnValue().SetUndefined();
return;
}
gboolean has_key = g_settings_schema_has_key (gsettings_schema, key);
if ( ! has_key ) {
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "Key does not exist!", NewStringType::kNormal ).ToLocalChecked() ));
g_free((void *) schema_id);
g_free((void *) key);
args.GetReturnValue().SetUndefined();
return;
}
// 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)) {
args.GetReturnValue().Set(Number::New(isolate, g_variant_get_double(variant)));
return;
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_INT32)) {
args.GetReturnValue().Set(Int32::New(isolate, g_variant_get_int32(variant)));
return;
}
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.
*/
args.GetReturnValue().Set(Number::New(isolate, gunit32_value));
return;
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_STRING)) {
args.GetReturnValue().Set(String::NewFromUtf8(isolate, g_variant_get_string(variant,NULL), String::NewStringType::kNormalString));
return;
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_STRING_ARRAY)) {
gsize length;
const gchar **elems = g_variant_get_strv(variant,&length);
Local<Array> nodes = Array::New(isolate);
for (unsigned int i = 0; i < length; ++i) {
nodes->Set(i, String::NewFromUtf8(isolate, elems[i], String::NewStringType::kNormalString));
}
g_free(elems);
args.GetReturnValue().Set(nodes);
return;
}
else if (g_variant_type_equal(type, X_G_VARIANT_TYPE_STRING_TUPLE_ARRAY)) {
Local<Array> tuple_array = Array::New(isolate);
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(isolate);
tuple->Set(0, String::NewFromUtf8(isolate, str1, String::NewStringType::kNormalString));
tuple->Set(1, String::NewFromUtf8(isolate, str2, String::NewStringType::kNormalString));
tuple_array->Set(i, tuple);
i++;
}
g_variant_iter_free (iter);
args.GetReturnValue().Set(tuple_array);
return;
}
else if (g_variant_type_equal(type,G_VARIANT_TYPE_BOOLEAN)) {
args.GetReturnValue().Set(Boolean::New(isolate, g_variant_get_boolean(variant)));
return;
}
else {
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "Need to implement reading that value type", NewStringType::kNormal ).ToLocalChecked() ));
args.GetReturnValue().SetUndefined();
return;
}
}
/* Takes schema_id string */
void schema_exists(const FunctionCallbackInfo<Value>& args) {
char *schema_id = v8_value_to_utf8_string(args[0]);
GSettingsSchemaSource *schema_source = g_settings_schema_source_get_default();
Isolate *isolate = args.GetIsolate();
if ( ! schema_source ) {
g_free(schema_id);
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "No schema sources available!", NewStringType::kNormal ).ToLocalChecked() ));
args.GetReturnValue().SetUndefined();
return;
}
GSettingsSchema * schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
g_free(schema_id);
if ( ! schema ) {
args.GetReturnValue().Set(Boolean::New(isolate, false));
} else {
args.GetReturnValue().Set(Boolean::New(isolate, true));
}
}
/* Takes schema_id, key, and value */
void set_gsetting(const FunctionCallbackInfo<Value>& args) {
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;
Isolate *isolate = args.GetIsolate();
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);
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "No schema source available!", NewStringType::kNormal).ToLocalChecked() ));
args.GetReturnValue().SetUndefined();
return;
}
GSettingsSchema * gsettings_schema =
g_settings_schema_source_lookup (schema_source,
schema_id,
FALSE);
if ( ! schema_source ) {
g_free((void *) schema_id);
g_free((void *) key);
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "Schema is not installed!", NewStringType::kNormal).ToLocalChecked() ));
args.GetReturnValue().SetUndefined();
return;
}
gboolean has_key = g_settings_schema_has_key (gsettings_schema, key);
if ( ! has_key ) {
g_free((void *) schema_id);
g_free((void *) key);
isolate->ThrowException(Exception::Error( String::NewFromOneByte(isolate, (const uint8_t*) "Key does not exist!", NewStringType::kNormal).ToLocalChecked() ));
args.GetReturnValue().SetUndefined();
return;
}
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(isolate)->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(isolate)->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( isolate->GetCurrentContext() ).ToLocalChecked()->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::NewFromOneByte(isolate, (const uint8_t*) "length", NewStringType::kNormal).ToLocalChecked())->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);
isolate->ThrowException(Exception::Error(String::NewFromOneByte(isolate, (const uint8_t*) "Array item have to be strings!", NewStringType::kNormal).ToLocalChecked()));
args.GetReturnValue().SetUndefined();
return;
}
}
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);
isolate->ThrowException(Exception::Error(String::NewFromOneByte(isolate, (const uint8_t*) validation_fail_message, NewStringType::kNormal).ToLocalChecked()));
args.GetReturnValue().SetUndefined();
return;
}
// write variant
gboolean is_valid = g_settings_schema_key_range_check (gsettings_key, variant_to_set);
if ( ! is_valid ) {
g_free((void *) key);
isolate->ThrowException(Exception::Error(String::NewFromOneByte(isolate, (const uint8_t*) "Invalid range or type!", NewStringType::kNormal).ToLocalChecked()));
args.GetReturnValue().SetUndefined();
return;
}
write_success = g_settings_set_value(settings, key, variant_to_set);
g_settings_sync();
g_free((void *) key);
if ( ! write_success ) {
isolate->ThrowException(Exception::Error(String::NewFromOneByte(isolate, (const uint8_t*) "Failed to set gsetting! Key is write protected.", NewStringType::kNormal).ToLocalChecked()));
}
args.GetReturnValue().SetUndefined();
return;
}
/* in addon initialization function */
void init(Local<Object> target) {
Isolate *isolate = target->GetIsolate();
target->Set(String::NewFromOneByte(isolate, (const uint8_t*) "set_gsetting", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, set_gsetting)->GetFunction());
target->Set(String::NewFromOneByte(isolate, (const uint8_t*) "get_gsetting", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, get_gsetting)->GetFunction());
target->Set(String::NewFromOneByte(isolate, (const uint8_t*) "get_gsetting_keys", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, get_gsetting_keys)->GetFunction());
target->Set(String::NewFromOneByte(isolate, (const uint8_t*) "schema_exists", NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, schema_exists)->GetFunction());
}
NODE_MODULE(nodegsettings, init)