Frage

I have an array stored as a GVariant of type a(ss) in GSettings, that I want to use in a Cinnamon Applet. I can retrieve the value successfully using the following code:

let schema = schema_source.lookup(SCHEMA_NAME, false);
let settings = new Gio.Settings({ settings_schema: schema });
let my_value = settings.get_value('myvalue');

but I can't unpack it. As far as I can see, I will probably need to unpack it using a GVariantIter structure, but the documentation is limited, and I can't find the correct interface in the gjs API (if, indeed, it exists). Does anyone know how to do it?

Thanks!

edit: my schema looks like this:

<key type="a(ss)" name="myvalue">
    <default>[]</default>
    <summary>an array of (string, string) tuples</summary>
    <description></description>
</key>

For the time being I'm using an external JSON file to store settings, but it's not a 100% satisfactory solution. I suppose I could maintain two as-type variables, and keep them aligned, but there must be a way to do this properly, right?

War es hilfreich?

Lösung

A bit late, but my_value.unpack() works absolutely fine.

my_value.deep_unpack() will recursively unpack the arrays and their elements.

Andere Tipps

From your type of setting I guess you want to store/retrieve an array of strings? In this case, there is an easier method using Gio.Settings.get_strv(String key):

// Read the array (will create a real JS array):
let string_array = settings.get_strv("myvalue");
// Now do something with it...
// Store it:
settings.set_strv("myvalue", string_array);
Gio.Settings.sync(); // Important!

In your schema, you would then include an entry like this:

<key name="myvalue" type="as">
  <default>[]</default>
  <summary>Some array.</summary>
  <description>An Array of strings.</description>
</key>

I use the same technique in my extension: Read/Write | Schema

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top