diff options
author | 1PunMan <saurabhsingh412@gmail.com> | 2018-05-28 13:36:42 +0530 |
---|---|---|
committer | Victor Toso <me@victortoso.com> | 2018-07-25 14:11:43 +0200 |
commit | b30608abacd37a4e39d5c5465efc82c566756631 (patch) | |
tree | 9234fa783c1f27a555fdce53a090dd3e4c9f4ec2 /src | |
parent | 06cd3944cc0a81a46c983f21a3bb6bc0266745c8 (diff) | |
download | grilo-b30608abacd37a4e39d5c5465efc82c566756631.tar.gz |
core: Add grl_data_set_for_id()
grl_data_set_for_id() allows keys that aren't registered,
to be registered and set it's value. It simply sets the value
for registered keys.
https://gitlab.gnome.org/GNOME/grilo/issues/1
Diffstat (limited to 'src')
-rw-r--r-- | src/data/grl-data.c | 69 | ||||
-rw-r--r-- | src/data/grl-data.h | 2 | ||||
-rw-r--r-- | src/grl-registry-priv.h | 4 | ||||
-rw-r--r-- | src/grl-registry.c | 68 |
4 files changed, 142 insertions, 1 deletions
diff --git a/src/data/grl-data.c b/src/data/grl-data.c index 3b20f44..68e5c4a 100644 --- a/src/data/grl-data.c +++ b/src/data/grl-data.c @@ -36,7 +36,7 @@ #include "grl-data.h" #include "grl-log.h" -#include <grl-registry.h> +#include "grl-registry-priv.h" #define GRL_LOG_DOMAIN_DEFAULT data_log_domain GRL_LOG_DOMAIN(data_log_domain); @@ -612,6 +612,73 @@ grl_data_get_int64 (GrlData *data, GrlKeyID key) } /** + * Returns whether the string is a canonical one. + **/ +static gboolean +is_canonical (const gchar *key) +{ + if (key == NULL) { + return FALSE; + } + + for (; *key != '\0'; key++) { + if (*key != '-' && + (*key < '0' || *key > '9') && + (*key < 'A' || *key > 'Z') && + (*key < 'a' || *key > 'z')) + return FALSE; + } + + return TRUE; +} + +/** + * grl_data_set_for_id: + * @data: data to change + * @key_name: name of the key to change or add + * @value: the new value + * + * Sets the first value associated with @key_name in @data. This @key_name is used to create + * a new #GParamSpec instance, which is further used to create and register a key using + * grl_registry_register_metadata_key(). If @key_name already has a first @value, old + * value is replaced by the new one. + * + * A property key_name consists of segments consisting of ASCII letters and + * digits, separated by either the '-' or '_' character. The first + * character of a property key_name must be a letter. Key_names which violate these + * rules lead to undefined behaviour. + * + * Returns: TRUE if @value was set to @key_name, FALSE otherwise. + * + * Since: 0.3.6 + **/ +gboolean +grl_data_set_for_id (GrlData *data, const gchar *key_name, const GValue *value) +{ + GrlRegistry *registry; + GrlKeyID key_id; + + key_name = g_intern_string (key_name); + g_return_val_if_fail (is_canonical (key_name), FALSE); + + registry = grl_registry_get_default (); + key_id = grl_registry_lookup_metadata_key (registry, key_name); + + if (key_id != GRL_METADATA_KEY_INVALID) { + grl_data_set (data, key_id, value); + return TRUE; + } + + GRL_DEBUG ("%s is not a registered metadata-key", key_name); + key_id = grl_registry_register_metadata_key_for_type(registry, key_name, G_VALUE_TYPE (value)); + if (key_id != GRL_METADATA_KEY_INVALID) + grl_data_set (data, key_id, value); + + return (key_id != GRL_METADATA_KEY_INVALID); +} + + +/** * grl_data_remove: * @data: data to change * @key: (type GrlKeyID): key to remove diff --git a/src/data/grl-data.h b/src/data/grl-data.h index ae95d99..7a400b6 100644 --- a/src/data/grl-data.h +++ b/src/data/grl-data.h @@ -115,6 +115,8 @@ void grl_data_set_boxed (GrlData *data, GrlKeyID key, gconstpointer boxed); void grl_data_set_int64 (GrlData *data, GrlKeyID key, gint64 intvalue); +gboolean grl_data_set_for_id (GrlData *data, const gchar *key_name, const GValue *value); + const GValue *grl_data_get (GrlData *data, GrlKeyID key); const gchar *grl_data_get_string (GrlData *data, GrlKeyID key); diff --git a/src/grl-registry-priv.h b/src/grl-registry-priv.h index b86a37c..e8eeef2 100644 --- a/src/grl-registry-priv.h +++ b/src/grl-registry-priv.h @@ -37,4 +37,8 @@ GrlKeyID grl_registry_register_metadata_key_system (GrlRegistry *registry, void grl_registry_shutdown (GrlRegistry *registry); +GrlKeyID grl_registry_register_metadata_key_for_type (GrlRegistry *registry, + const gchar *key_name, + GType type); + #endif /* _GRL_REGISTRY_PRIV_H_ */ diff --git a/src/grl-registry.c b/src/grl-registry.c index 6a1106f..a6189b7 100644 --- a/src/grl-registry.c +++ b/src/grl-registry.c @@ -565,6 +565,74 @@ grl_registry_register_metadata_key_full (GrlRegistry *registry, return registered_key; } +G_GNUC_INTERNAL GrlKeyID +grl_registry_register_metadata_key_for_type (GrlRegistry *registry, + const gchar *key_name, + GType type) +{ + GParamSpec *spec; + + switch (type) { + case G_TYPE_INT: + spec = g_param_spec_int (key_name, + key_name, + key_name, + 0, G_MAXINT, + 0, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + break; + + case G_TYPE_INT64: + spec = g_param_spec_int64 (key_name, + key_name, + key_name, + -1, G_MAXINT64, + -1, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + break; + + case G_TYPE_STRING: + spec = g_param_spec_string (key_name, + key_name, + key_name, + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + break; + + case G_TYPE_BOOLEAN: + spec = g_param_spec_boolean (key_name, + key_name, + key_name, + FALSE, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + break; + + case G_TYPE_FLOAT: + spec = g_param_spec_float (key_name, + key_name, + key_name, + 0, G_MAXFLOAT, + 0, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + break; + + default: + if (type == G_TYPE_DATE_TIME) { + spec = g_param_spec_boxed (key_name, + key_name, + key_name, + G_TYPE_DATE_TIME, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + } else { + GRL_WARNING ("'%s' is being ignored as G_TYPE '%s' is not being handled", + key_name, G_VALUE_TYPE_NAME (type)); + return GRL_METADATA_KEY_INVALID; + } + } + + return grl_registry_register_metadata_key (registry, spec, GRL_METADATA_KEY_INVALID, NULL); +} + static void key_id_handler_init (struct KeyIDHandler *handler) { |