summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-release.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-03-17 16:16:59 +0000
committerRichard Hughes <richard@hughsie.com>2014-03-17 16:19:35 +0000
commitda96a9dfdc8c09589f95790c8bd0c801ad067735 (patch)
tree936727f7d3dac1f9c8bd99c5d91834039f87ef47 /libappstream-glib/as-release.c
parent262bb60bd98a78cb07d3ea725909326f718ff6dc (diff)
downloadappstream-glib-da96a9dfdc8c09589f95790c8bd0c801ad067735.tar.gz
Allow releases to have a localized description
Additionally, only create the hash table of data if there is actual data.
Diffstat (limited to 'libappstream-glib/as-release.c')
-rw-r--r--libappstream-glib/as-release.c56
1 files changed, 44 insertions, 12 deletions
diff --git a/libappstream-glib/as-release.c b/libappstream-glib/as-release.c
index f880031..f75fec6 100644
--- a/libappstream-glib/as-release.c
+++ b/libappstream-glib/as-release.c
@@ -25,14 +25,15 @@
#include "as-node.h"
#include "as-release.h"
+#include "as-tag.h"
#include "as-utils.h"
typedef struct _AsReleasePrivate AsReleasePrivate;
struct _AsReleasePrivate
{
- gchar *version;
- gchar *description;
- guint64 timestamp;
+ gchar *version;
+ GHashTable *descriptions;
+ guint64 timestamp;
};
G_DEFINE_TYPE_WITH_PRIVATE (AsRelease, as_release, G_TYPE_OBJECT)
@@ -49,7 +50,8 @@ as_release_finalize (GObject *object)
AsReleasePrivate *priv = GET_PRIVATE (release);
g_free (priv->version);
- g_free (priv->description);
+ if (priv->descriptions != NULL)
+ g_hash_table_unref (priv->descriptions);
G_OBJECT_CLASS (as_release_parent_class)->finalize (object);
}
@@ -109,18 +111,21 @@ as_release_get_timestamp (AsRelease *release)
/**
* as_release_get_description:
* @release: a #AsRelease instance.
+ * @locale: the locale, or %NULL. e.g. "en_GB"
*
- * Gets the release description markup.
+ * Gets the release description markup for a given locale.
*
* Returns: markup
*
* Since: 0.1.0
**/
const gchar *
-as_release_get_description (AsRelease *release)
+as_release_get_description (AsRelease *release, const gchar *locale)
{
AsReleasePrivate *priv = GET_PRIVATE (release);
- return priv->description;
+ if (locale == NULL)
+ locale = "C";
+ return g_hash_table_lookup (priv->descriptions, locale);
}
/**
@@ -161,6 +166,7 @@ as_release_set_timestamp (AsRelease *release, guint64 timestamp)
/**
* as_release_set_description:
* @release: a #AsRelease instance.
+ * @locale: the locale, or %NULL. e.g. "en_GB"
* @description: the description markup.
* @description_len: the size of @description, or -1 if %NULL-terminated.
*
@@ -170,11 +176,22 @@ as_release_set_timestamp (AsRelease *release, guint64 timestamp)
**/
void
as_release_set_description (AsRelease *release,
+ const gchar *locale,
const gchar *description,
gssize description_len)
{
AsReleasePrivate *priv = GET_PRIVATE (release);
- priv->description = as_strndup (description, description_len);
+ if (locale == NULL)
+ locale = "C";
+ if (priv->descriptions == NULL) {
+ priv->descriptions = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ g_free,
+ g_free);
+ }
+ g_hash_table_insert (priv->descriptions,
+ g_strdup (locale),
+ as_strndup (description, description_len));
}
/**
@@ -197,11 +214,15 @@ as_release_node_insert (AsRelease *release, GNode *parent)
timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT,
priv->timestamp);
- n = as_node_insert (parent, "release", priv->description,
+ n = as_node_insert (parent, "release", NULL,
AS_NODE_INSERT_FLAG_NONE,
"timestamp", timestamp_str,
"version", priv->version,
NULL);
+ if (priv->descriptions != NULL) {
+ as_node_insert_localized (n, "description", priv->descriptions,
+ AS_NODE_INSERT_FLAG_PRE_ESCAPED);
+ }
g_free (timestamp_str);
return n;
}
@@ -222,15 +243,26 @@ gboolean
as_release_node_parse (AsRelease *release, GNode *node, GError **error)
{
const gchar *tmp;
+ GNode *n;
+ GString *xml;
+
tmp = as_node_get_attribute (node, "timestamp");
if (tmp != NULL)
as_release_set_timestamp (release, atol (tmp));
tmp = as_node_get_attribute (node, "version");
if (tmp != NULL)
as_release_set_version (release, tmp, -1);
- tmp = as_node_get_data (node);
- if (tmp != NULL)
- as_release_set_description (release, tmp, -1);
+
+ /* descriptions are translated and optional */
+ for (n = node->children; n != NULL; n = n->next) {
+ if (as_tag_from_string (as_node_get_name (n)) != AS_TAG_DESCRIPTION)
+ continue;
+ xml = as_node_to_xml (n->children, AS_NODE_TO_XML_FLAG_NONE);
+ as_release_set_description (release,
+ as_node_get_attribute (n, "xml:lang"),
+ xml->str, xml->len);
+ g_string_free (xml, TRUE);
+ }
return TRUE;
}