1

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?

simon
  • 15,344
  • 5
  • 45
  • 67

2 Answers2

3

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

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

Artless
  • 4,522
  • 1
  • 25
  • 40
  • Works for me! -- May I ask where you found this piece of information? In the [official docs](https://developer.gnome.org/glib/stable/glib-GVariant.html) I cannot find a method named `unpack()`. – JayStrictor Mar 09 '16 at 00:35
0

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

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • thanks for your answer. unfortunately, i need to store an array of _pairs_ of strings, hence `a(ss)`, and hence the problem. i've update my question a little. – simon Dec 15 '12 at 19:56