## GSettingsBackend

GSettingsBackend — Interface for settings backend implementations

## Synopsis

    #include <gio/gsettingsbackend.h>

                        GSettingsBackend;
    struct              GSettingsBackendClass;
    #define             G_SETTINGS_BACKEND_EXTENSION_POINT_NAME
    GSettingsBackend *  g_settings_backend_get_default      (void);
    void                g_settings_backend_changed          (GSettingsBackend *backend,
                                                             const gchar *key,
                                                             gpointer origin_tag);
    void                g_settings_backend_path_changed     (GSettingsBackend *backend,
                                                             const gchar *path,
                                                             gpointer origin_tag);
    void                g_settings_backend_keys_changed     (GSettingsBackend *backend,
                                                             const gchar *path,
                                                             gchar const * const *items,
                                                             gpointer origin_tag);
    void                g_settings_backend_path_writable_changed
                                                            (GSettingsBackend *backend,
                                                             const gchar *path);
    void                g_settings_backend_writable_changed (GSettingsBackend *backend,
                                                             const gchar *key);
    void                g_settings_backend_changed_tree     (GSettingsBackend *backend,
                                                             GTree *tree,
                                                             gpointer origin_tag);
    void                g_settings_backend_flatten_tree     (GTree *tree,
                                                             gchar **path,
                                                             const gchar ***keys,
                                                             GVariant ***values);
    GSettingsBackend *  g_keyfile_settings_backend_new      (const gchar *filename,
                                                             const gchar *root_path,
                                                             const gchar *root_group);
    GSettingsBackend *  g_memory_settings_backend_new       (void);
    GSettingsBackend *  g_null_settings_backend_new         (void);

## Object Hierarchy

      GObject
       +----GSettingsBackend

## Description

The GSettingsBackend interface defines a generic interface for non-strictly-typed data that is stored in a hierarchy. To implement an alternative storage backend for GSettings, you need to implement the GSettingsBackend interface and then make it implement the extension point G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.

The interface defines methods for reading and writing values, a method for determining if writing of certain values will fail (lockdown) and a change notification mechanism.

The semantics of the interface are very precisely defined and implementations must carefully adhere to the expectations of callers that are documented on each of the interface methods.

Some of the GSettingsBackend functions accept or return a GTree. These trees always have strings as keys and GVariant as values. `g_settings_backend_create_tree()` is a convenience function to create suitable trees.

### Note

The GSettingsBackend API is exported to allow third-party implementations, but does not carry the same stability guarantees as the public GIO API. For this reason, you have to define the C preprocessor symbol G_SETTINGS_ENABLE_BACKEND before including `gio/gsettingsbackend.h`

## Details

### GSettingsBackend

    typedef struct _GSettingsBackend GSettingsBackend;

An implementation of a settings storage repository.

### struct GSettingsBackendClass

    struct GSettingsBackendClass {
      GObjectClass parent_class;

      GVariant *    (*read)             (GSettingsBackend    *backend,
                                         const gchar         *key,
                                         const GVariantType  *expected_type,
                                         gboolean             default_value);

      gboolean      (*get_writable)     (GSettingsBackend    *backend,
                                         const gchar         *key);

      gboolean      (*write)            (GSettingsBackend    *backend,
                                         const gchar         *key,
                                         GVariant            *value,
                                         gpointer             origin_tag);
      gboolean      (*write_tree)       (GSettingsBackend    *backend,
                                         GTree               *tree,
                                         gpointer             origin_tag);
      void          (*reset)            (GSettingsBackend    *backend,
                                         const gchar         *key,
                                         gpointer             origin_tag);

      void          (*subscribe)        (GSettingsBackend    *backend,
                                         const gchar         *name);
      void          (*unsubscribe)      (GSettingsBackend    *backend,
                                         const gchar         *name);
      void          (*sync)             (GSettingsBackend    *backend);

      GPermission * (*get_permission)   (GSettingsBackend    *backend,
                                         const gchar         *path);

      gpointer padding[24];
    };

### G_SETTINGS_BACKEND_EXTENSION_POINT_NAME

    #define G_SETTINGS_BACKEND_EXTENSION_POINT_NAME "gsettings-backend"

Extension point for GSettingsBackend functionality.

### g_settings_backend_get_default ()

    GSettingsBackend *  g_settings_backend_get_default      (void);

Returns the default GSettingsBackend. It is possible to override the default by setting the `GSETTINGS_BACKEND` environment variable to the name of a settings backend.

The user gets a reference to the backend.

Returns : | the default GSettingsBackend. [transfer full]

Since 2.28

### g_settings_backend_changed ()

    void                g_settings_backend_changed          (GSettingsBackend *backend,
                                                             const gchar *key,
                                                             gpointer origin_tag);

Signals that a single key has possibly changed. Backend implementations should call this if a key has possibly changed its value.

`key` must be a valid key (ie starting with a slash, not containing '//', and not ending with a slash).

The implementation must call this function during any call to `g_settings_backend_write()`, before the call returns (except in the case that no keys are actually changed and it cares to detect this fact). It may not rely on the existence of a mainloop for dispatching the signal later.

The implementation may call this function at any other time it likes in response to other events (such as changes occurring outside of the program). These calls may originate from a mainloop or may originate in response to any other action (including from calls to `g_settings_backend_write()`).

In the case that this call is in response to a call to `g_settings_backend_write()` then `origin_tag` must be set to the same value that was passed to that call.

`backend` : | a GSettingsBackend implementation
`key` : | the name of the key
`origin_tag` : | the origin tag

Since 2.26

### g_settings_backend_path_changed ()

    void                g_settings_backend_path_changed     (GSettingsBackend *backend,
                                                             const gchar *path,
                                                             gpointer origin_tag);

Signals that all keys below a given path may have possibly changed. Backend implementations should call this if an entire path of keys have possibly changed their values.

`path` must be a valid path (ie starting and ending with a slash and not containing '//').

The meaning of this signal is that any of the key which has a name starting with `path` may have changed.

The same rules for when notifications must occur apply as per `g_settings_backend_changed()`. This call might be an appropriate reasponse to a 'reset' call but implementations are also free to explicitly list the keys that were affected by that call if they can easily do so.

For efficiency reasons, the implementation should strive for `path` to be as long as possible (ie: the longest common prefix of all of the keys that were changed) but this is not strictly required. As an example, if this function is called with the path of "/" then every single key in the application will be notified of a possible change.

`backend` : | a GSettingsBackend implementation
`path` : | the path containing the changes
`origin_tag` : | the origin tag

Since 2.26

### g_settings_backend_keys_changed ()

    void                g_settings_backend_keys_changed     (GSettingsBackend *backend,
                                                             const gchar *path,
                                                             gchar const * const *items,
                                                             gpointer origin_tag);

Signals that a list of keys have possibly changed. Backend implementations should call this if keys have possibly changed their values.

`path` must be a valid path (ie starting and ending with a slash and not containing '//'). Each string in `items` must form a valid key name when `path` is prefixed to it (ie: each item must not start or end with '/' and must not contain '//').

The meaning of this signal is that any of the key names resulting from the contatenation of `path` with each item in `items` may have changed.

The same rules for when notifications must occur apply as per `g_settings_backend_changed()`. These two calls can be used interchangeably if exactly one item has changed (although in that case `g_settings_backend_changed()` is definitely preferred).

For efficiency reasons, the implementation should strive for `path` to be as long as possible (ie: the longest common prefix of all of the keys that were changed) but this is not strictly required.

`backend` : | a GSettingsBackend implementation
`path` : | the path containing the changes
`items` : | the `NULL`-terminated list of changed keys. [array zero-terminated=1]
`origin_tag` : | the origin tag

Since 2.26

### g_settings_backend_path_writable_changed ()

    void                g_settings_backend_path_writable_changed
                                                            (GSettingsBackend *backend,
                                                             const gchar *path);

Signals that the writability of all keys below a given path may have changed.

Since GSettings performs no locking operations for itself, this call will always be made in response to external events.

`backend` : | a GSettingsBackend implementation
`path` : | the name of the path

Since 2.26

### g_settings_backend_writable_changed ()

    void                g_settings_backend_writable_changed (GSettingsBackend *backend,
                                                             const gchar *key);

Signals that the writability of a single key has possibly changed.

Since GSettings performs no locking operations for itself, this call will always be made in response to external events.

`backend` : | a GSettingsBackend implementation
`key` : | the name of the key

Since 2.26

### g_settings_backend_changed_tree ()

    void                g_settings_backend_changed_tree     (GSettingsBackend *backend,
                                                             GTree *tree,
                                                             gpointer origin_tag);

This call is a convenience wrapper. It gets the list of changes from `tree`, computes the longest common prefix and calls `g_settings_backend_changed()`.

`backend` : | a GSettingsBackend implementation
`tree` : | a GTree containing the changes
`origin_tag` : | the origin tag

Since 2.26

### g_settings_backend_flatten_tree ()

    void                g_settings_backend_flatten_tree     (GTree *tree,
                                                             gchar **path,
                                                             const gchar ***keys,
                                                             GVariant ***values);

Calculate the longest common prefix of all keys in a tree and write out an array of the key names relative to that prefix and, optionally, the value to store at each of those keys.

You must free the value returned in `path`, `keys` and `values` using `g_free()`. You should not attempt to free or unref the contents of `keys` or `values`.

`tree` : | a GTree containing the changes
`path` : | the location to save the path. [out]
`keys` : | the location to save the relative keys. [out][transfer container][array zero-terminated=1]
`values` : | the location to save the values, or `NULL`. [out][allow-none][transfer container][array zero-terminated=1]

Since 2.26

### g_keyfile_settings_backend_new ()

    GSettingsBackend *  g_keyfile_settings_backend_new      (const gchar *filename,
                                                             const gchar *root_path,
                                                             const gchar *root_group);

Creates a keyfile-backed GSettingsBackend.

The filename of the keyfile to use is given by `filename`.

All settings read to or written from the backend must fall under the path given in `root_path` (which must start and end with a slash and not contain two consecutive slashes). `root_path` may be "/".

If `root_group` is non-`NULL` then it specifies the name of the keyfile group used for keys that are written directly below `root_path`. For example, if `root_path` is "/apps/example/" and `root_group` is "toplevel", then settings the key "/apps/example/enabled" to a value of `TRUE` will cause the following to appear in the keyfile:

    1
    2

    [toplevel]
    enabled=true

If `root_group` is `NULL` then it is not permitted to store keys directly below the `root_path`.

For keys not stored directly below `root_path` (ie: in a sub-path), the name of the subpath (with the final slash stripped) is used as the name of the keyfile group. To continue the example, if "/apps/example/profiles/default/font-size" were set to 12 then the following would appear in the keyfile:

    1
    2

    [profiles/default]
    font-size=12

The backend will refuse writes (and return writability as being `FALSE`) for keys outside of `root_path` and, in the event that `root_group` is `NULL`, also for keys directly under `root_path`. Writes will also be refused if the backend detects that it has the inability to rewrite the keyfile (ie: the containing directory is not writable).

There is no checking done for your key namespace clashing with the syntax of the key file format. For example, if you have '[' or ']' characters in your path names or '=' in your key names you may be in trouble.

`filename` : | the filename of the keyfile
`root_path` : | the path under which all settings keys appear
`root_group` : | the group name corresponding to `root_path`, or `NULL`. [allow-none]
Returns : | a keyfile-backed GSettingsBackend. [transfer full]

### g_memory_settings_backend_new ()

    GSettingsBackend *  g_memory_settings_backend_new       (void);

Creates a memory-backed GSettingsBackend.

This backend allows changes to settings, but does not write them to any backing storage, so the next time you run your application, the memory backend will start out with the default values again.

Returns : | a newly created GSettingsBackend. [transfer full]

Since 2.28

### g_null_settings_backend_new ()

    GSettingsBackend *  g_null_settings_backend_new         (void);

Creates a readonly GSettingsBackend.

This backend does not allow changes to settings, so all settings will always have their default values.

Returns : | a newly created GSettingsBackend. [transfer full]

Since 2.28

## See Also

GSettings, GIOExtensionPoint