diff options
author | Benjamin Berg <bberg@redhat.com> | 2019-01-23 00:15:37 +0100 |
---|---|---|
committer | Benjamin Berg <bberg@redhat.com> | 2019-01-29 12:05:43 +0100 |
commit | b789a1cda2a65427c1b8b77739011badcb557afa (patch) | |
tree | 2611ece4c22f7882b0be63a3c1a987309cafbd98 | |
parent | afe8661704542db012be71beeaeac5345a0c5485 (diff) | |
download | gnome-control-center-b789a1cda2a65427c1b8b77739011badcb557afa.tar.gz |
common: Add CcValueObject until HdyValueObject is available
libhandy now has HdyValueObject, this simply copies the class over as it
will only become available in libhandy 0.8.
-rw-r--r-- | panels/common/cc-value-object.c | 269 | ||||
-rw-r--r-- | panels/common/cc-value-object.h | 30 | ||||
-rw-r--r-- | panels/common/meson.build | 2 |
3 files changed, 301 insertions, 0 deletions
diff --git a/panels/common/cc-value-object.c b/panels/common/cc-value-object.c new file mode 100644 index 000000000..c779b3a0b --- /dev/null +++ b/panels/common/cc-value-object.c @@ -0,0 +1,269 @@ +/* + * Copyright (C) 2019 Red Hat Inc. + * + * SPDX-License-Identifier: LGPL-2.1+ + * + * This is a copy of CcValueObject from libhandy 0.8. + * Modified to change the prefix and to not translate the property nick and + * description. + */ + +#include <gobject/gvaluecollector.h> +#include "cc-value-object.h" + +/** + * SECTION:cc-value-object + * @short_description: An object representing a #GValue. + * @Title: CcValueObject + * + * The #CcValueObject object represents a #GValue, allowing it to be + * used with #GListModel. + * + * Since: 0.0.8 + */ + +struct _CcValueObject +{ + GObject parent_instance; + + GValue value; +}; + +G_DEFINE_TYPE (CcValueObject, cc_value_object, G_TYPE_OBJECT) + +enum { + PROP_0, + PROP_VALUE, + N_PROPS +}; + +static GParamSpec *props [N_PROPS]; + +/** + * cc_value_object_new: + * @value: the #GValue to store + * + * Create a new #CcValueObject. + * + * Returns: a new #CcValueObject + * Since: 0.0.8 + */ +CcValueObject * +cc_value_object_new (const GValue *value) +{ + return g_object_new (CC_TYPE_VALUE_OBJECT, + "value", value, + NULL); +} + +/** + * cc_value_object_new_collect: (skip) + * @type: the #GType of the value + * @...: the value to store + * + * Creates a new #CcValueObject. This is a convenience method which uses + * the G_VALUE_COLLECT() macro internally. + * + * Returns: a new #CcValueObject + * Since: 0.0.8 + */ +CcValueObject* +cc_value_object_new_collect (GType type, ...) +{ + g_auto(GValue) value = G_VALUE_INIT; + g_autofree gchar *error = NULL; + va_list var_args; + + va_start (var_args, type); + + G_VALUE_COLLECT_INIT (&value, type, var_args, 0, &error); + + va_end (var_args); + + if (error) + g_critical ("%s: %s", G_STRFUNC, error); + + return g_object_new (CC_TYPE_VALUE_OBJECT, + "value", &value, + NULL); +} + +/** + * cc_value_object_new_string: (skip) + * @string: (transfer none): the string to store + * + * Creates a new #CcValueObject. This is a convenience method to create a + * #CcValueObject that stores a string. + * + * Returns: a new #CcValueObject + * Since: 0.0.8 + */ +CcValueObject* +cc_value_object_new_string (const gchar *string) +{ + g_auto(GValue) value = G_VALUE_INIT; + + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, string); + return cc_value_object_new (&value); +} + +/** + * cc_value_object_new_take_string: (skip) + * @string: (transfer full): the string to store + * + * Creates a new #CcValueObject. This is a convenience method to create a + * #CcValueObject that stores a string taking ownership of it. + * + * Returns: a new #CcValueObject + * Since: 0.0.8 + */ +CcValueObject* +cc_value_object_new_take_string (gchar *string) +{ + g_auto(GValue) value = G_VALUE_INIT; + + g_value_init (&value, G_TYPE_STRING); + g_value_take_string (&value, string); + return cc_value_object_new (&value); +} + +static void +cc_value_object_finalize (GObject *object) +{ + CcValueObject *self = CC_VALUE_OBJECT (object); + + g_value_unset (&self->value); + + G_OBJECT_CLASS (cc_value_object_parent_class)->finalize (object); +} + +static void +cc_value_object_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + CcValueObject *self = CC_VALUE_OBJECT (object); + + switch (prop_id) + { + case PROP_VALUE: + g_value_set_boxed (value, &self->value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +cc_value_object_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + CcValueObject *self = CC_VALUE_OBJECT (object); + GValue *real_value; + + switch (prop_id) + { + case PROP_VALUE: + /* construct only */ + real_value = g_value_get_boxed (value); + g_value_init (&self->value, real_value->g_type); + g_value_copy (real_value, &self->value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +cc_value_object_class_init (CcValueObjectClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = cc_value_object_finalize; + object_class->get_property = cc_value_object_get_property; + object_class->set_property = cc_value_object_set_property; + + props[PROP_VALUE] = + g_param_spec_boxed ("value", "Value", + "The contained value", + G_TYPE_VALUE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, + N_PROPS, + props); +} + +static void +cc_value_object_init (CcValueObject *self) +{ +} + +/** + * cc_value_object_get_value: + * @value: the #CcValueObject + * + * Return the contained value. + * + * Returns: (transfer none): the contained #GValue + * Since: 0.0.8 + */ +const GValue* +cc_value_object_get_value (CcValueObject *value) +{ + return &value->value; +} + +/** + * cc_value_object_copy_value: + * @value: the #CcValueObject + * @dest: #GValue with correct type to copy into + * + * Copy data from the contained #GValue into @dest. + * + * Since: 0.0.8 + */ +void +cc_value_object_copy_value (CcValueObject *value, + GValue *dest) +{ + g_value_copy (&value->value, dest); +} + +/** + * cc_value_object_get_string: + * @value: the #CcValueObject + * + * Returns the contained string if the value is of type #G_TYPE_STRING. + * + * Returns: (transfer none): the contained string + * Since: 0.0.8 + */ +const gchar* +cc_value_object_get_string (CcValueObject *value) +{ + return g_value_get_string (&value->value); +} + +/** + * cc_value_object_dup_string: + * @value: the #CcValueObject + * + * Returns a copy of the contained string if the value is of type + * #G_TYPE_STRING. + * + * Returns: (transfer full): a copy of the contained string + * Since: 0.0.8 + */ +gchar* +cc_value_object_dup_string (CcValueObject *value) +{ + return g_value_dup_string (&value->value); +} + diff --git a/panels/common/cc-value-object.h b/panels/common/cc-value-object.h new file mode 100644 index 000000000..b39c2061c --- /dev/null +++ b/panels/common/cc-value-object.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2019 Red Hat Inc. + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#pragma once + +#include <gio/gio.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define CC_TYPE_VALUE_OBJECT (cc_value_object_get_type()) + +G_DECLARE_FINAL_TYPE (CcValueObject, cc_value_object, CC, VALUE_OBJECT, GObject) + +CcValueObject *cc_value_object_new (const GValue *value); +CcValueObject *cc_value_object_new_collect (GType type, + ...); +CcValueObject *cc_value_object_new_string (const gchar *string); +CcValueObject *cc_value_object_new_take_string (gchar *string); + +const GValue* cc_value_object_get_value (CcValueObject *value); +void cc_value_object_copy_value (CcValueObject *value, + GValue *dest); +const gchar* cc_value_object_get_string (CcValueObject *value); +gchar* cc_value_object_dup_string (CcValueObject *value); + +G_END_DECLS diff --git a/panels/common/meson.build b/panels/common/meson.build index 7d15e64f3..118a84257 100644 --- a/panels/common/meson.build +++ b/panels/common/meson.build @@ -26,6 +26,8 @@ common_sources += gnome.mkenums( sources = files( 'cc-hostname-entry.c', + # Remove once we depend on libhandy 0.8 + 'cc-value-object.c', 'hostname-helper.c', 'list-box-helper.c', ) |