![]() |
![]() |
![]() |
GIO Reference Manual | ![]() |
---|---|---|---|---|
Top | Description | Object Hierarchy | Properties | Signals |
GSettings; GSettings * g_settings_new (const gchar *schema
); GSettings * g_settings_new_with_path (const gchar *schema
,const gchar *path
); GSettings * g_settings_new_with_context (const gchar *schema
,const gchar *context
); GSettings * g_settings_new_with_context_and_path (const gchar *schema
,const gchar *context
,const gchar *path
); gboolean g_settings_supports_context (const gchar *context
); GVariant * g_settings_get_value (GSettings *settings
,const gchar *key
); gboolean g_settings_set_value (GSettings *settings
,const gchar *key
,GVariant *value
); gboolean g_settings_is_writable (GSettings *settings
,const gchar *name
); void g_settings_delay (GSettings *settings
); void g_settings_apply (GSettings *settings
); void g_settings_revert (GSettings *settings
); gboolean g_settings_get_has_unapplied (GSettings *settings
); GSettings * g_settings_get_child (GSettings *settings
,const gchar *name
); void g_settings_get (GSettings *settings
,const gchar *key
,const gchar *format
,...
); gboolean g_settings_set (GSettings *settings
,const gchar *key
,const gchar *format
,...
); gboolean g_settings_get_boolean (GSettings *settings
,const gchar *key
); gboolean g_settings_set_boolean (GSettings *settings
,const gchar *key
,gboolean value
); gint g_settings_get_int (GSettings *settings
,const gchar *key
); gboolean g_settings_set_int (GSettings *settings
,const gchar *key
,gint value
); gdouble g_settings_get_double (GSettings *settings
,const gchar *key
); gboolean g_settings_set_double (GSettings *settings
,const gchar *key
,gdouble value
); gchar * g_settings_get_string (GSettings *settings
,const gchar *key
); gboolean g_settings_set_string (GSettings *settings
,const gchar *key
,const gchar *value
); gchar ** g_settings_get_strv (GSettings *settings
,const gchar *key
,gsize *length
); gboolean g_settings_set_strv (GSettings *settings
,const gchar *key
,const gchar *const *value
,gssize length
); enum GSettingsBindFlags; void g_settings_bind (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,GSettingsBindFlags flags
); void g_settings_bind_with_mapping (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,GSettingsBindFlags flags
,GSettingsBindGetMapping get_mapping
,GSettingsBindSetMapping set_mapping
,gpointer user_data
,GDestroyNotify destroy
); void g_settings_bind_writable (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,gboolean inverted
); void g_settings_unbind (gpointer object
,const gchar *property
); GVariant * (*GSettingsBindSetMapping) (const GValue *value
,const GVariantType *expected_type
,gpointer user_data
); gboolean (*GSettingsBindGetMapping) (GValue *value
,GVariant *variant
,gpointer user_data
);
"context" gchar* : Read / Write / Construct Only "has-unapplied" gboolean : Read "path" gchar* : Read / Write / Construct Only "schema" gchar* : Read / Write / Construct Only
"change-event" : Run Last "changed" : Run Last / Has Details "writable-change-event" : Run Last "writable-changed" : Run Last / Has Details
The GSettings class provides a convenient API for storing and retrieving application settings.
When creating a GSettings instance, you have to specify a schema that describes the keys in your settings and their types and default values, as well as some other information.
Normally, a schema has as fixed path that determines where the settings are stored in the conceptual global tree of settings. However, schemas can also be 'relocatable', i.e. not equipped with a fixed path. This is useful e.g. when the schema describes an 'account', and you want to be able to store a arbitrary number of accounts.
Unlike other configuration systems (like GConf), GSettings does not restrict keys to basic types like strings and numbers. GSettings stores values as GVariant, and allows any GVariantType for keys. Key names are restricted to lowercase characters, numbers and '-'. Furthermore, the names must begin with a lowercase character, must not end with a '-', and must not contain consecutive dashes. Key names can be up to 32 characters long.
Similar to GConf, the default values in GSettings schemas can be
localized, but the localized values are stored in gettext catalogs
and looked up with the domain that is specified in the
gettext-domain
attribute of the
<schemalist>
or <schema>
elements and the category that is specified in the l10n attribute of the
<key>
element.
GSettings uses schemas in a compact binary form that is created by the gschema-compile utility. The input is a schema description in an XML format that can be described by the following DTD:
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 |
<!ELEMENT schemalist (schema*) > <!ATTLIST schemalist gettext-domain #IMPLIED > <!ELEMENT schema (key|child)* > <!ATTLIST schema id CDATA #REQUIRED path CDATA #IMPLIED gettext-domain CDATA #IMPLIED > <!ELEMENT key (default|summary?|description?|range?|choices?) > <!-- name can only contain lowercase letters, numbers and '-' --> <!-- type must be a GVariant type string --> <!ATTLIST key name CDATA #REQUIRED type CDATA #REQUIRED > <!-- the default value is specified a a serialized GVariant, i.e. you have to include the quotes when specifying a string --> <!ELEMENT default (#PCDATA) > <!-- the presence of the l10n attribute marks a default value for translation, its value is the gettext category to use --> <!-- if context is present, it specifies msgctxt to use --> <!ATTLIST default l10n (messages|time) #IMPLIED context CDATA #IMPLIED > <!ELEMENT summary (#PCDATA) > <!ELEMENT description (#PCDATA) > <!ELEMENT range (min,max) > <!ELEMENT min (#PCDATA) > <!ELEMENT max (#PCDATA) > <!ELEMENT choices (choice+) > <!ELEMENT choice (alias?) > <!ATTLIST choice value CDATA #REQUIRED > <!ELEMENT choice (alias?) > <!ELEMENT alias EMPTY > <!ATTLIST alias value CDATA #REQUIRED > <!ELEMENT child EMPTY > <!ATTLIST child name CDATA #REQUIRED schema CDATA #REQUIRED > |
At runtime, schemas are identified by their id (as specified
in the id
attribute of the
<schema>
element). The
convention for schema ids is to use a dotted name, similar in
style to a DBus bus name, e.g. "org.gnome.font-rendering".
A very convenient feature of GSettings lets you bind GObject properties
directly to settings, using g_settings_bind()
. Once a GObject property
has been bound to a setting, changes on either side are automatically
propagated to the other side. GSettings handles details like
mapping between GObject and GVariant types, and preventing infinite
cycles.
This makes it very easy to hook up a preferences dialog to the underlying settings. To make this even more convenient, GSettings looks for a boolean property with the name "sensitivity" and automatically binds it to the writability of the bound setting. If this 'magic' gets in the way, it can be suppressed with the G_SETTINGS_BIND_NO_SENSITIVITY flag.
GSettings * g_settings_new (const gchar *schema
);
Creates a new GSettings object with a given schema.
|
the name of the schema |
Returns : |
a new GSettings object |
Since 2.26
GSettings * g_settings_new_with_path (const gchar *schema
,const gchar *path
);
Creates a new GSettings object with a given schema and path.
You only need to do this if you want to directly create a settings object with a schema that doesn't have a specified path of its own. That's quite rare.
It is a programmer error to call this function for a schema that has an explicitly specified path.
|
the name of the schema |
|
the path to use |
Returns : |
a new GSettings object |
Since 2.26
GSettings * g_settings_new_with_context (const gchar *schema
,const gchar *context
);
Creates a new GSettings object with a given schema and context.
Creating settings objects with a context allow accessing settings from a database other than the usual one. For example, it may make sense to specify "defaults" in order to get a settings object that modifies the system default settings instead of the settings for this user.
It is a programmer error to call this function for an unsupported
context. Use g_settings_supports_context()
to determine if a context
is supported if you are unsure.
|
the name of the schema |
|
the context to use |
Returns : |
a new GSettings object |
Since 2.26
GSettings * g_settings_new_with_context_and_path (const gchar *schema
,const gchar *context
,const gchar *path
);
Creates a new GSettings object with a given schema, context and path.
This is a mix of g_settings_new_with_context()
and
g_settings_new_with_path()
.
|
the name of the schema |
|
|
|
the path to use |
Returns : |
a new GSettings object |
Since 2.26
gboolean g_settings_supports_context (const gchar *context
);
|
|
Returns : |
GVariant * g_settings_get_value (GSettings *settings
,const gchar *key
);
Gets the value that is stored in settings
for key
.
It is a programmer error to give a key
that isn't valid for
settings
.
Since 2.26
gboolean g_settings_set_value (GSettings *settings
,const gchar *key
,GVariant *value
);
Sets key
in settings
to value
.
It is a programmer error to give a key
that isn't valid for
settings
. It is a programmer error to give a value
of the
incorrect type.
If value
is floating then this function consumes the reference.
|
a GSettings object |
|
the name of the key to set |
|
a GVariant of the correct type |
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gboolean g_settings_is_writable (GSettings *settings
,const gchar *name
);
Finds out if a key can be written or not
Since 2.26
void g_settings_delay (GSettings *settings
);
Changes the GSettings object into 'delay-apply' mode. In this
mode, changes to settings
are not immediately propagated to the
backend, but kept locally until g_settings_apply()
is called.
|
a GSettings object |
Since 2.26
void g_settings_apply (GSettings *settings
);
Applies any changes that have been made to the settings. This
function does nothing unless settings
is in 'delay-apply' mode;
see g_settings_set_delay_apply()
. In the normal case settings are
always applied immediately.
|
a GSettings instance |
void g_settings_revert (GSettings *settings
);
Reverts all non-applied changes to the settings. This function
does nothing unless settings
is in 'delay-apply' mode; see
g_settings_set_delay_apply()
. In the normal case settings are
always applied immediately.
Change notifications will be emitted for affected keys.
|
a GSettings instance |
gboolean g_settings_get_has_unapplied (GSettings *settings
);
Returns whether the GSettings object has any unapplied changes. This can only be the case if it is in 'delayed-apply' mode.
Since 2.26
GSettings * g_settings_get_child (GSettings *settings
,const gchar *name
);
Creates a 'child' settings object which has a base path of
base-path
/name
", where
base-path
is the base path of settings
.
The schema for the child settings object must have been declared
in the schema of settings
using a <child>
element.
|
a GSettings object |
|
the name of the 'child' schema |
Returns : |
a 'child' settings object |
Since 2.26
void g_settings_get (GSettings *settings
,const gchar *key
,const gchar *format
,...
);
Gets the value that is stored at key
in settings
.
A convenience function that combines g_settings_get_value()
with
g_variant_get()
.
It is a programmer error to pass a key
that isn't valid for
settings
or a format_string
that doesn't match the type of key
according
to the schema of settings
.
|
a GSettings object |
|
the key to get the value for |
|
a GVariant format string |
|
arguments as per format
|
Since 2.26
gboolean g_settings_set (GSettings *settings
,const gchar *key
,const gchar *format
,...
);
Sets key
in settings
to value
.
A convenience function that combines g_settings_set_value()
with
g_variant_new()
.
It is a programmer error to pass a key
that isn't valid for
settings
or a format
that doesn't match the type of key
according
to the schema of settings
.
|
a GSettings object |
|
the name of the key to set |
|
a GVariant format string |
|
arguments as per format
|
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gboolean g_settings_get_boolean (GSettings *settings
,const gchar *key
);
Gets the value that is stored at key
in settings
.
A convenience variant of g_settings_get()
for booleans.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type boolean.
|
a GSettings object |
|
the key to get the value for |
Returns : |
a boolean |
Since 2.26
gboolean g_settings_set_boolean (GSettings *settings
,const gchar *key
,gboolean value
);
Sets key
in settings
to value
.
A convenience variant of g_settings_set()
for booleans.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type boolean.
|
a GSettings object |
|
the name of the key to set |
|
the value to set it to |
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gint g_settings_get_int (GSettings *settings
,const gchar *key
);
Gets the value that is stored at key
in settings
.
A convenience variant of g_settings_get()
for 32-bit integers.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type int32.
|
a GSettings object |
|
the key to get the value for |
Returns : |
an integer |
Since 2.26
gboolean g_settings_set_int (GSettings *settings
,const gchar *key
,gint value
);
Sets key
in settings
to value
.
A convenience variant of g_settings_set()
for 32-bit integers.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type int32.
|
a GSettings object |
|
the name of the key to set |
|
the value to set it to |
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gdouble g_settings_get_double (GSettings *settings
,const gchar *key
);
Gets the value that is stored at key
in settings
.
A convenience variant of g_settings_get()
for doubles.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type double.
|
a GSettings object |
|
the key to get the value for |
Returns : |
a double |
Since 2.26
gboolean g_settings_set_double (GSettings *settings
,const gchar *key
,gdouble value
);
Sets key
in settings
to value
.
A convenience variant of g_settings_set()
for doubles.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type double.
|
a GSettings object |
|
the name of the key to set |
|
the value to set it to |
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gchar * g_settings_get_string (GSettings *settings
,const gchar *key
);
Gets the value that is stored at key
in settings
.
A convenience variant of g_settings_get()
for strings.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type string.
|
a GSettings object |
|
the key to get the value for |
Returns : |
a newly-allocated string |
Since 2.26
gboolean g_settings_set_string (GSettings *settings
,const gchar *key
,const gchar *value
);
Sets key
in settings
to value
.
A convenience variant of g_settings_set()
for strings.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type string.
|
a GSettings object |
|
the name of the key to set |
|
the value to set it to |
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
gchar ** g_settings_get_strv (GSettings *settings
,const gchar *key
,gsize *length
);
Gets the value that is stored at key
in settings
.
A convenience variant of g_settings_get()
for string arrays.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type 'string array'.
|
a GSettings object |
|
the key to get the value for |
|
|
Returns : |
a newly-allocated, NULL -terminated array of strings
|
Since 2.26
gboolean g_settings_set_strv (GSettings *settings
,const gchar *key
,const gchar *const *value
,gssize length
);
Sets key
in settings
to value
.
A convenience variant of g_settings_set()
for string arrays.
It is a programmer error to pass a key
that isn't valid for
settings
or is not of type 'string array'.
|
a GSettings object |
|
the name of the key to set |
|
the value to set it to |
|
|
Returns : |
TRUE if setting the key succeeded,
FALSE if the key was not writable
|
Since 2.26
typedef enum { G_SETTINGS_BIND_DEFAULT, G_SETTINGS_BIND_GET = (1<<0), G_SETTINGS_BIND_SET = (1<<1), G_SETTINGS_BIND_NO_SENSITIVITY = (1<<2), G_SETTINGS_BIND_GET_NO_CHANGES = (1<<3) } GSettingsBindFlags;
Flags used when creating a binding. These flags determine in which direction the binding works. The default is to synchronize in both directions.
Equivalent to G_SETTINGS_BIND_GET|G_SETTINGS_BIND_SET
|
|
Update the GObject property when the setting changes. It is an error to use this flag if the property is not writable. | |
Update the setting when the GObject property changes. It is an error to use this flag if the property is not readable. | |
Do not try to bind a "sensitivity" property to the writability of the setting | |
When set in addition to G_SETTINGS_BIND_GET, set the GObject property value initially from the setting, but do not listen for changes of the setting |
void g_settings_bind (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,GSettingsBindFlags flags
);
Create a binding between the key
in the settings
object
and the property property
of object
.
The binding uses the default GIO mapping functions to map
between the settings and property values. These functions
handle booleans, numeric types and string types in a
straightforward way. Use g_settings_bind_with_mapping()
if
you need a custom mapping, or map between types that are not
supported by the default mapping functions.
Unless the flags
include G_SETTINGS_BIND_NO_SENSITIVITY
, this
function also establishes a binding between the writability of
key
and the "sensitive" property of object
(if object
has
a boolean property by that name). See g_settings_bind_writable()
for more details about writable bindings.
Note that the lifecycle of the binding is tied to the object, and that you can have only one binding per object property. If you bind the same property twice on the same object, the second binding overrides the first one.
|
a GSettings object |
|
the key to bind |
|
a GObject |
|
the name of the property to bind |
|
flags for the binding |
Since 2.26
void g_settings_bind_with_mapping (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,GSettingsBindFlags flags
,GSettingsBindGetMapping get_mapping
,GSettingsBindSetMapping set_mapping
,gpointer user_data
,GDestroyNotify destroy
);
Create a binding between the key
in the settings
object
and the property property
of object
.
The binding uses the provided mapping functions to map between settings and property values.
Note that the lifecycle of the binding is tied to the object, and that you can have only one binding per object property. If you bind the same property twice on the same object, the second binding overrides the first one.
|
a GSettings object |
|
the key to bind |
|
a GObject |
|
the name of the property to bind |
|
flags for the binding |
|
a function that gets called to convert values
from settings to object , or NULL to use the default GIO mapping
|
|
a function that gets called to convert values
from object to settings , or NULL to use the default GIO mapping
|
|
data that gets passed to get_mapping and set_mapping
|
|
GDestroyNotify function for user_data
|
Since 2.26
void g_settings_bind_writable (GSettings *settings
,const gchar *key
,gpointer object
,const gchar *property
,gboolean inverted
);
Create a binding between the writability of key
in the
settings
object and the property property
of object
.
The property must be boolean; "sensitive" or "visible"
properties of widgets are the most likely candidates.
Writable bindings are always uni-directional; changes of the writability of the setting will be propagated to the object property, not the other way.
When the inverted
argument is TRUE
, the binding inverts the
value as it passes from the setting to the object, i.e. property
will be set to TRUE
if the key is not
writable.
Note that the lifecycle of the binding is tied to the object, and that you can have only one binding per object property. If you bind the same property twice on the same object, the second binding overrides the first one.
|
a GSettings object |
|
the key to bind |
|
a GObject |
|
the name of a boolean property to bind |
|
whether to 'invert' the value |
Since 2.26
void g_settings_unbind (gpointer object
,const gchar *property
);
Removes an existing binding for property
on object
.
Note that bindings are automatically removed when the object is finalized, so it is rarely necessary to call this function.
|
the object |
|
the property whose binding is removed |
Since 2.26
GVariant * (*GSettingsBindSetMapping) (const GValue *value
,const GVariantType *expected_type
,gpointer user_data
);
The type for the function that is used to convert an object property value to a GVariant for storing it in GSettings.
|
a GValue containing the property value to map |
|
the GVariantType to create |
|
user data that was specified when the binding was created |
Returns : |
a new GVariant holding the data from value ,
or NULL in case of an error
|
"context"
property"context" gchar* : Read / Write / Construct Only
The name of the context that the settings are stored in.
Default value: ""
"has-unapplied"
property"has-unapplied" gboolean : Read
If this property is TRUE
, the GSettings object has outstanding
changes that will be applied when g_settings_apply()
is called.
Default value: FALSE
"path"
property"path" gchar* : Read / Write / Construct Only
The path within the backend where the settings are stored.
Default value: NULL
"change-event"
signalgboolean user_function (GSettings *settings, gpointer keys, gint n_keys, gpointer user_data) : Run Last
The "change-event" signal is emitted once per change event that affects this settings object. You should connect to this signal only if you are interested in viewing groups of changes before they are split out into multiple emissions of the "changed" signal. For most use cases it is more appropriate to use the "changed" signal.
In the event that the change event applies to one or more specified
keys, keys
will be an array of GQuark of length n_keys
. In the
event that the change event applies to the GSettings object as a
whole (ie: potentially every key has been changed) then keys
will
be NULL
and n_keys
will be 0.
The default handler for this signal invokes the "changed" signal
for each affected key. If any other connected handler returns
TRUE
then this default functionality will be supressed.
|
the object on which the signal was emitted |
|
an array of GQuarks for the changed keys, or NULL
|
|
the length of the keys array, or 0
|
|
TRUE to stop other handlers from being invoked for the
event. FALSE to propagate the event further.
|
|
user data set when the signal handler was connected. |
"changed"
signalvoid user_function (GSettings *settings, gchar *key, gpointer user_data) : Run Last / Has Details
The "changed" signal is emitted when a key has potentially changed.
You should call one of the g_settings_get()
calls to check the new
value.
This signal supports detailed connections. You can connect to the detailed signal "changed::x" in order to only receive callbacks when key "x" changes.
|
the object on which the signal was emitted |
|
the name of the key that changed |
|
user data set when the signal handler was connected. |
"writable-change-event"
signalgboolean user_function (GSettings *settings, guint key, gpointer user_data) : Run Last
The "writable-change-event" signal is emitted once per writability change event that affects this settings object. You should connect to this signal if you are interested in viewing groups of changes before they are split out into multiple emissions of the "writable-changed" signal. For most use cases it is more appropriate to use the "writable-changed" signal.
In the event that the writability change applies only to a single
key, key
will be set to the GQuark for that key. In the event
that the writability change affects the entire settings object,
key
will be 0.
The default handler for this signal invokes the "writable-changed"
and "changed" signals for each affected key. This is done because
changes in writability might also imply changes in value (if for
example, a new mandatory setting is introduced). If any other
connected handler returns TRUE
then this default functionality
will be supressed.
|
the object on which the signal was emitted |
|
the quark of the key, or 0 |
|
TRUE to stop other handlers from being invoked for the
event. FALSE to propagate the event further.
|
|
user data set when the signal handler was connected. |
"writable-changed"
signalvoid user_function (GSettings *settings, gchar *key, gpointer user_data) : Run Last / Has Details
The "writable-changed" signal is emitted when the writability of a
key has potentially changed. You should call
g_settings_is_writable()
in order to determine the new status.
This signal supports detailed connections. You can connect to the detailed signal "writable-changed::x" in order to only receive callbacks when the writability of "x" changes.
|
the object on which the signal was emitted |
|
the key |
|
user data set when the signal handler was connected. |