libmalcontent-ui: Use appstream-glib code for content rating systems

If a suitably new version of appstream-glib is available, use its
implementation of content rating systems (see
https://github.com/hughsie/appstream-glib/pull/364), rather than our
forked one.

This adds a dependency on libappstream-glib, but no particular version.

Eventually, our copy of `gs-content-rating.[ch]` can be dropped.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Fixes: #7
This commit is contained in:
Philip Withnall 2020-05-21 16:16:21 +01:00
parent 12c1718782
commit 52399e8d58
6 changed files with 63 additions and 6 deletions

View File

@ -6,7 +6,7 @@ before_script:
libglib2.0-dev libgirepository1.0-dev libpam0g-dev
gettext policykit-1 libpolkit-gobject-1-dev git
lcov libgtk-3-dev libaccountsservice-dev libflatpak-dev
libglib-testing-0-dev
libglib-testing-0-dev libappstream-glib-dev
- export LANG=C.UTF-8
stages:

View File

@ -89,6 +89,7 @@ Dependencies
------------
* accounts-service
* appstream-glib
* dbus-daemon
* flatpak
* gio-2.0 ≥ 2.60

View File

@ -21,11 +21,13 @@
#include "config.h"
#include <appstream-glib.h>
#include <glib/gi18n-lib.h>
#include <string.h>
#include "gs-content-rating.h"
#if !AS_CHECK_VERSION(0, 7, 18)
const gchar *
gs_content_rating_system_to_str (GsContentRatingSystem system)
{
@ -573,7 +575,9 @@ static const struct {
{ "violence-desecration", OARS_1_1, 0, 13, 15, 18 },
{ "violence-slavery", OARS_1_1, 0, 13, 15, 18 },
};
#endif /* appstream-glib < 0.7.18 */
#if !AS_CHECK_VERSION(0, 7, 15)
/**
* as_content_rating_id_value_to_csm_age:
* @id: the subsection ID e.g. "violence-cartoon"
@ -616,7 +620,9 @@ as_content_rating_id_value_to_csm_age (const gchar *id, MctAppFilterOarsValue va
/* @id not found. */
return 0;
}
#endif /* appstream-glib < 0.7.15 */
#if !AS_CHECK_VERSION(0, 7, 18)
/**
* as_content_rating_id_csm_age_to_value:
* @id: the subsection ID e.g. "violence-cartoon"
@ -646,3 +652,4 @@ as_content_rating_id_csm_age_to_value (const gchar *id, guint age)
return MCT_APP_FILTER_OARS_VALUE_UNKNOWN;
}
#endif /* appstream-glib < 0.7.18 */

View File

@ -23,9 +23,37 @@
G_BEGIN_DECLS
#include <appstream-glib.h>
#include <glib-object.h>
#include <libmalcontent/malcontent.h>
#if AS_CHECK_VERSION(0, 7, 18)
#define GS_CONTENT_RATING_SYSTEM_UNKNOWN AS_CONTENT_RATING_SYSTEM_UNKNOWN
#define GS_CONTENT_RATING_SYSTEM_INCAA AS_CONTENT_RATING_SYSTEM_INCAA
#define GS_CONTENT_RATING_SYSTEM_ACB AS_CONTENT_RATING_SYSTEM_ACB
#define GS_CONTENT_RATING_SYSTEM_DJCTQ AS_CONTENT_RATING_SYSTEM_DJCTQ
#define GS_CONTENT_RATING_SYSTEM_GSRR AS_CONTENT_RATING_SYSTEM_GSRR
#define GS_CONTENT_RATING_SYSTEM_PEGI AS_CONTENT_RATING_SYSTEM_PEGI
#define GS_CONTENT_RATING_SYSTEM_KAVI AS_CONTENT_RATING_SYSTEM_KAVI
#define GS_CONTENT_RATING_SYSTEM_USK AS_CONTENT_RATING_SYSTEM_USK
#define GS_CONTENT_RATING_SYSTEM_ESRA AS_CONTENT_RATING_SYSTEM_ESRA
#define GS_CONTENT_RATING_SYSTEM_CERO AS_CONTENT_RATING_SYSTEM_CERO
#define GS_CONTENT_RATING_SYSTEM_OFLCNZ AS_CONTENT_RATING_SYSTEM_OFLCNZ
#define GS_CONTENT_RATING_SYSTEM_RUSSIA AS_CONTENT_RATING_SYSTEM_RUSSIA
#define GS_CONTENT_RATING_SYSTEM_MDA AS_CONTENT_RATING_SYSTEM_MDA
#define GS_CONTENT_RATING_SYSTEM_GRAC AS_CONTENT_RATING_SYSTEM_GRAC
#define GS_CONTENT_RATING_SYSTEM_ESRB AS_CONTENT_RATING_SYSTEM_ESRB
#define GS_CONTENT_RATING_SYSTEM_IARC AS_CONTENT_RATING_SYSTEM_IARC
#define GS_CONTENT_RATING_SYSTEM_LAST AS_CONTENT_RATING_SYSTEM_LAST
#define GsContentRatingSystem AsContentRatingSystem
#define gs_utils_content_rating_age_to_str as_content_rating_system_format_age
#define gs_utils_content_rating_system_from_locale as_content_rating_system_from_locale
#define gs_content_rating_system_to_str as_content_rating_system_to_string
#define gs_utils_content_rating_get_values as_content_rating_system_get_formatted_ages
#define gs_utils_content_rating_get_ages as_content_rating_system_get_csm_ages
#define as_content_rating_id_csm_age_to_value as_content_rating_attribute_from_csm_age
#else
typedef enum {
GS_CONTENT_RATING_SYSTEM_UNKNOWN,
GS_CONTENT_RATING_SYSTEM_INCAA,
@ -53,7 +81,13 @@ GsContentRatingSystem gs_utils_content_rating_system_from_locale (const gchar *l
const gchar *gs_content_rating_system_to_str (GsContentRatingSystem system);
gchar **gs_utils_content_rating_get_values (GsContentRatingSystem system);
const guint *gs_utils_content_rating_get_ages (GsContentRatingSystem system, gsize *length_out);
guint as_content_rating_id_value_to_csm_age (const gchar *id, MctAppFilterOarsValue value);
MctAppFilterOarsValue as_content_rating_id_csm_age_to_value (const gchar *id, guint age);
#endif /* appstream-glib < 0.7.18 */
#if AS_CHECK_VERSION(0, 7, 15)
#define as_content_rating_id_value_to_csm_age as_content_rating_attribute_to_csm_age
#else
guint as_content_rating_id_value_to_csm_age (const gchar *id, MctAppFilterOarsValue value);
#endif /* appstream-glib < 0.7.15 */
G_END_DECLS

View File

@ -36,6 +36,7 @@ libmalcontent_ui_public_deps = [
libmalcontent_dep,
]
libmalcontent_ui_private_deps = [
dependency('appstream-glib'),
dependency('flatpak'),
]

View File

@ -22,6 +22,7 @@
#include "config.h"
#include <appstream-glib.h>
#include <libmalcontent/malcontent.h>
#include <locale.h>
#include <gio/gio.h>
@ -166,8 +167,7 @@ static const GActionEntry actions[] = {
{ "set-age", on_set_age_action_activated, "u", NULL, NULL, { 0, }}
};
/* FIXME: Factor this out and rely on code from libappstream-glib or gnome-software
* to do it. See: https://gitlab.freedesktop.org/pwithnall/malcontent/issues/7 */
#if !AS_CHECK_VERSION(0, 7, 15)
static const gchar * const oars_categories[] =
{
"violence-cartoon",
@ -199,6 +199,7 @@ static const gchar * const oars_categories[] =
"money-gambling",
NULL
};
#endif /* appstream-glib < 0.7.15 */
/* Auxiliary methods */
@ -372,6 +373,13 @@ oars_value_to_string (MctAppFilterOarsValue oars_value)
}
}
/* Ensure the enum casts below are safe. */
G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_UNKNOWN == (int) AS_CONTENT_RATING_VALUE_UNKNOWN);
G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_NONE == (int) AS_CONTENT_RATING_VALUE_NONE);
G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_MILD == (int) AS_CONTENT_RATING_VALUE_MILD);
G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_MODERATE == (int) AS_CONTENT_RATING_VALUE_MODERATE);
G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_INTENSE == (int) AS_CONTENT_RATING_VALUE_INTENSE);
static void
update_oars_level (MctUserControls *self)
{
@ -380,6 +388,9 @@ update_oars_level (MctUserControls *self)
guint maximum_age;
gsize i;
gboolean all_categories_unset;
#if AS_CHECK_VERSION(0, 7, 15)
g_autofree const gchar **oars_categories = as_content_rating_get_all_rating_ids ();
#endif
g_assert (self->filter != NULL);
@ -393,7 +404,7 @@ update_oars_level (MctUserControls *self)
oars_value = mct_app_filter_get_oars_value (self->filter, oars_categories[i]);
all_categories_unset &= (oars_value == MCT_APP_FILTER_OARS_VALUE_UNKNOWN);
age = as_content_rating_id_value_to_csm_age (oars_categories[i], oars_value);
age = as_content_rating_id_value_to_csm_age (oars_categories[i], (AsContentRatingValue) oars_value);
g_debug ("OARS value for '%s': %s", oars_categories[i], oars_value_to_string (oars_value));
@ -1484,6 +1495,9 @@ mct_user_controls_build_app_filter (MctUserControls *self,
{
gboolean restrict_web_browsers;
gsize i;
#if AS_CHECK_VERSION(0, 7, 15)
g_autofree const gchar **oars_categories = as_content_rating_get_all_rating_ids ();
#endif
g_return_if_fail (MCT_IS_USER_CONTROLS (self));
g_return_if_fail (builder != NULL);
@ -1509,7 +1523,7 @@ mct_user_controls_build_app_filter (MctUserControls *self,
const gchar *oars_category;
oars_category = oars_categories[i];
oars_value = as_content_rating_id_csm_age_to_value (oars_category, self->selected_age);
oars_value = (MctAppFilterOarsValue) as_content_rating_id_csm_age_to_value (oars_category, self->selected_age);
g_debug ("\t\t → %s: %s", oars_category, oars_value_to_string (oars_value));