From e4e38f9788da6bf88607660f48df0038cc13b56e Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Wed, 21 Sep 2016 08:05:06 +0100 Subject: Add as_content_rating_get_value() This allows us to get the specific enumerated value for a classification ID which we can use in gnome-software to explain the rating to the user. --- libappstream-glib/as-content-rating.c | 24 ++++++++++++++++++++++++ libappstream-glib/as-content-rating.h | 2 ++ libappstream-glib/as-self-test.c | 6 ++++++ 3 files changed, 32 insertions(+) diff --git a/libappstream-glib/as-content-rating.c b/libappstream-glib/as-content-rating.c index 6ec9cfd..488fdc9 100644 --- a/libappstream-glib/as-content-rating.c +++ b/libappstream-glib/as-content-rating.c @@ -84,6 +84,30 @@ as_content_rating_class_init (AsContentRatingClass *klass) object_class->finalize = as_content_rating_finalize; } +/** + * as_content_rating_get_value: + * @content_rating: a #AsContentRating + * @id: A ratings ID, e.g. `violence-bloodshed`. + * + * Gets the set value of a content rating key. + * + * Returns: the #AsContentRatingValue, or %AS_CONTENT_RATING_VALUE_UNKNOWN + * + * Since: 0.6.4 + **/ +AsContentRatingValue +as_content_rating_get_value (AsContentRating *content_rating, const gchar *id) +{ + AsContentRatingPrivate *priv = GET_PRIVATE (content_rating); + guint i; + for (i = 0; i < priv->keys->len; i++) { + AsContentRatingKey *key = g_ptr_array_index (priv->keys, i); + if (g_strcmp0 (key->id, id) == 0) + return key->value; + } + return AS_CONTENT_RATING_VALUE_UNKNOWN; +} + /** * as_content_rating_value_to_string: * @value: the #AsContentRatingValue. diff --git a/libappstream-glib/as-content-rating.h b/libappstream-glib/as-content-rating.h index ba5ed1a..2c4117e 100644 --- a/libappstream-glib/as-content-rating.h +++ b/libappstream-glib/as-content-rating.h @@ -76,6 +76,8 @@ AsContentRatingValue as_content_rating_value_from_string (const gchar *value); /* getters */ const gchar *as_content_rating_get_kind (AsContentRating *content_rating); guint as_content_rating_get_minimum_age (AsContentRating *content_rating); +AsContentRatingValue as_content_rating_get_value (AsContentRating *content_rating, + const gchar *id); /* setters */ void as_content_rating_set_kind (AsContentRating *content_rating, diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 892ad28..5aff325 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -1465,6 +1465,12 @@ as_test_content_rating_func (void) /* verify */ g_assert_cmpstr (as_content_rating_get_kind (content_rating), ==, "oars-1.0"); + g_assert_cmpint (as_content_rating_get_value (content_rating, "drugs-alcohol"), ==, + AS_CONTENT_RATING_VALUE_MODERATE); + g_assert_cmpint (as_content_rating_get_value (content_rating, "violence-cartoon"), ==, + AS_CONTENT_RATING_VALUE_MILD); + g_assert_cmpint (as_content_rating_get_value (content_rating, "violence-bloodshed"), ==, + AS_CONTENT_RATING_VALUE_UNKNOWN); as_node_unref (root); /* check CSM */ -- cgit v1.2.1