GSettings schema for a(sv)

I’m trying to save print and page settings using gsettings.
( from gtk_print_settings_to_gvariant() )
In my program.gschema.xml file I have …

        <key name="print-settings" type="a(sv)">
            <default></default>
            <summary>GTK print settings</summary>
            <description>The print settings that were last used</description>
        </key>
        <key name="page-settings" type="a(sv)">
            <default></default>
            <summary>GTK page settings</summary>
            <description>The page settings that were last used</description>
        </key>

but when I compile this I get the error:

😼 glib-compile-schemas --dry-run .
./HPGLplotter.gschema.xml:8:1  Failed to parse <default> value of type “a(sv)”: 0:expected value.  This entire file has been ignored.
😼

What do you use as the default value for an array like this?

… to answer my own question…

<default>[]</default>

If you’re trying to serialise a key/value store, you might want the GVariant type a{sv} instead of a(sv) as it better represents one.

Just a suggestion to consider; I don’t know what your particular use case is.

What is the difference ?
… actually when I checked the type that gtk_print_settings_to_gvariant() returned it was a{sv} !

{} is a string dictionary element. () is a tuple.

See GLib.VariantType

1 Like

I still have a problem. I’m getting an error when I use g_settings_set_value().
I get this error on either print or page settings

Print Settings is of type a{sv}
Print Setup is of type a{sv}
Bail out! GLib:ERROR:../glib/gvarianttypeinfo.c:190:g_variant_type_info_check: assertion failed: (0 <= index && index < 24)

FWICT it should be correct…

My schema is …

<schemalist>
  <schema id="us.heterodyne.HPGLplotter" path="/us/heterodyne/HPGLplotter/">
    <key name="print-settings" type="a{sv}">
      <default>[]</default>
      <summary>GTK print settings</summary>
      <description>
        GTK print settings
      </description>
    </key>
    <key name="page-setup" type="a{sv}">
      <default>[]</default>
      <summary>GTK page setup</summary>
      <description>
        GTK page setup
      </description>
    </key>
    <key name="pen-colors" type="a(dddd)">
      <default>[]</default>
      <summary>HPGL pen colors</summary>
      <description>
        HPGL pen colors (RGBA)
      </description>
    </key>

and my code is

	GVariant *gvPrintSettings = NULL, *gvPageSetup = NULL, *gvPenColors;
    GVariantBuilder *builder;

    GSettings *gs;

    if( !g_settings_schema_exist( GSETTINGS_SCHEMA ) )
    	return FALSE;

    gs = g_settings_new( GSETTINGS_SCHEMA );
    if( pGlobal->printSettings )
    	gvPrintSettings = gtk_print_settings_to_gvariant( pGlobal->printSettings );
    if( pGlobal->pageSetup )
    	gvPageSetup  = gtk_page_setup_to_gvariant( pGlobal->pageSetup );

    g_print( "Print Settings is of type %s\n", g_variant_get_type_string( gvPrintSettings ));
    if( gvPrintSettings )
    	g_settings_set_value( gs, "print-settings", gvPrintSettings );

    g_print( "Print Setup is of type %s\n", g_variant_get_type_string( gvPageSetup ));
    if( gvPageSetup )
    	g_settings_set_value( gs, "page-setup", gvPageSetup );

OK … it was just my (very) thin understanding…

I was freeing the gvariant’s later on with

    g_variant_unref (gvPrintSettings);
    g_variant_unref (gvPageSetup);

On looking at the documentation I see that g_settings_set_value says …

If value is floating then this function consumes the reference.

… and indeed the gtk_print_settings_to_gvariant() and gtk_page_setup_to_gvariant() functions give floating references.

1 Like

Yes, floating references are something you have to be aware of when working with GVariants. They can make using them in C code a lot simpler, but can be a footgun if you’re not aware of them, unfortunately.

Glad you got it working

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.