diff options
author | Richard Hughes <richard@hughsie.com> | 2015-10-02 21:29:30 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2015-10-02 21:29:42 +0100 |
commit | 5331fc26935211fd48e13bcf3cf5f8caa3cc0ae6 (patch) | |
tree | eb1520d846a1dc469d572f193ed83626f9983bd6 /libappstream-glib | |
parent | 1f7d4bf70aab6e2344cafa6a9b968dfc905f1be6 (diff) | |
download | appstream-glib-5331fc26935211fd48e13bcf3cf5f8caa3cc0ae6.tar.gz |
Add support for <size>
This is used on release objects for installed and downloaded size.
Diffstat (limited to 'libappstream-glib')
-rw-r--r-- | libappstream-glib/as-enums.c | 42 | ||||
-rw-r--r-- | libappstream-glib/as-enums.h | 21 | ||||
-rw-r--r-- | libappstream-glib/as-release.c | 75 | ||||
-rw-r--r-- | libappstream-glib/as-release.h | 5 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 9 | ||||
-rw-r--r-- | libappstream-glib/as-tag.c | 1 | ||||
-rw-r--r-- | libappstream-glib/as-tag.gperf | 1 | ||||
-rw-r--r-- | libappstream-glib/as-tag.h | 2 |
8 files changed, 153 insertions, 3 deletions
diff --git a/libappstream-glib/as-enums.c b/libappstream-glib/as-enums.c index d028ccb..08f9945 100644 --- a/libappstream-glib/as-enums.c +++ b/libappstream-glib/as-enums.c @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- * - * Copyright (C) 2014 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2014-2015 Richard Hughes <richard@hughsie.com> * * Licensed under the GNU Lesser General Public License Version 2.1 * @@ -213,6 +213,46 @@ as_kudo_kind_from_string (const gchar *kudo_kind) } /** + * as_size_kind_to_string: + * @size_kind: the #AsSizeKind. + * + * Converts the enumerated value to an text representation. + * + * Returns: string version of @size_kind + * + * Since: 0.5.2 + **/ +const gchar * +as_size_kind_to_string (AsSizeKind size_kind) +{ + if (size_kind == AS_SIZE_KIND_INSTALLED) + return "installed"; + if (size_kind == AS_SIZE_KIND_DOWNLOAD) + return "download"; + return "unknown"; +} + +/** + * as_size_kind_from_string: + * @size_kind: the string. + * + * Converts the text representation to an enumerated value. + * + * Returns: a #AsSizeKind or %AS_SIZE_KIND_UNKNOWN for unknown + * + * Since: 0.5.2 + **/ +AsSizeKind +as_size_kind_from_string (const gchar *size_kind) +{ + if (g_strcmp0 (size_kind, "installed") == 0) + return AS_SIZE_KIND_INSTALLED; + if (g_strcmp0 (size_kind, "download") == 0) + return AS_SIZE_KIND_DOWNLOAD; + return AS_SIZE_KIND_UNKNOWN; +} + +/** * as_urgency_kind_to_string: * @urgency_kind: the #AsUrgencyKind. * diff --git a/libappstream-glib/as-enums.h b/libappstream-glib/as-enums.h index ae3be23..06496e4 100644 --- a/libappstream-glib/as-enums.h +++ b/libappstream-glib/as-enums.h @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- * - * Copyright (C) 2014 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2014-2015 Richard Hughes <richard@hughsie.com> * * Licensed under the GNU Lesser General Public License Version 2.1 * @@ -128,6 +128,25 @@ typedef enum { AS_URGENCY_KIND_LAST } AsUrgencyKind; +/** + * AsSizeKind: + * @AS_SIZE_KIND_UNKNOWN: Not known + * @AS_SIZE_KIND_INSTALLED: Installed size + * @AS_SIZE_KIND_DOWNLOAD: Download size + * + * The release size kind. + **/ +typedef enum { + AS_SIZE_KIND_UNKNOWN, /* Since: 0.5.2 */ + AS_SIZE_KIND_INSTALLED, /* Since: 0.5.2 */ + AS_SIZE_KIND_DOWNLOAD, /* Since: 0.5.2 */ + /*< private >*/ + AS_SIZE_KIND_LAST +} AsSizeKind; + +const gchar *as_size_kind_to_string (AsSizeKind size_kind); +AsSizeKind as_size_kind_from_string (const gchar *size_kind); + const gchar *as_urgency_kind_to_string (AsUrgencyKind urgency_kind); AsUrgencyKind as_urgency_kind_from_string (const gchar *urgency_kind); diff --git a/libappstream-glib/as-release.c b/libappstream-glib/as-release.c index bcb00f1..d551cf5 100644 --- a/libappstream-glib/as-release.c +++ b/libappstream-glib/as-release.c @@ -48,6 +48,7 @@ typedef struct { AsUrgencyKind urgency; + guint64 size[AS_SIZE_KIND_LAST]; gchar *version; GHashTable *descriptions; guint64 timestamp; @@ -84,9 +85,13 @@ static void as_release_init (AsRelease *release) { AsReleasePrivate *priv = GET_PRIVATE (release); + guint i; + priv->urgency = AS_URGENCY_KIND_UNKNOWN; priv->locations = g_ptr_array_new_with_free_func (g_free); priv->checksums = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + for (i = 0; i < AS_SIZE_KIND_LAST; i++) + priv->size[i] = 0; } /** @@ -131,6 +136,45 @@ as_release_vercmp (AsRelease *rel1, AsRelease *rel2) } /** + * as_release_get_size: + * @release: a #AsRelease instance + * @kind: a #AsSizeKind, e.g. #AS_SIZE_KIND_DOWNLOAD + * + * Gets the release size. + * + * Returns: The size in bytes, or 0 for unknown. + * + * Since: 0.5.2 + **/ +guint64 +as_release_get_size (AsRelease *release, AsSizeKind kind) +{ + AsReleasePrivate *priv = GET_PRIVATE (release); + if (kind >= AS_SIZE_KIND_LAST) + return 0; + return priv->size[kind]; +} + +/** + * as_release_set_size: + * @release: a #AsRelease instance + * @kind: a #AsSizeKind, e.g. #AS_SIZE_KIND_DOWNLOAD + * @size: a size in bytes, or 0 for unknown + * + * Sets the release size. + * + * Since: 0.5.2 + **/ +void +as_release_set_size (AsRelease *release, AsSizeKind kind, guint64 size) +{ + AsReleasePrivate *priv = GET_PRIVATE (release); + if (kind >= AS_SIZE_KIND_LAST) + return; + priv->size[kind] = size; +} + +/** * as_release_get_urgency: * @release: a #AsRelease instance. * @@ -439,6 +483,7 @@ as_release_node_insert (AsRelease *release, GNode *parent, AsNodeContext *ctx) AsReleasePrivate *priv = GET_PRIVATE (release); AsChecksum *checksum; GNode *n; + guint i; n = as_node_insert (parent, "release", NULL, AS_NODE_INSERT_FLAG_NONE, @@ -457,7 +502,6 @@ as_release_node_insert (AsRelease *release, GNode *parent, AsNodeContext *ctx) as_node_add_attribute (n, "version", priv->version); if (as_node_context_get_version (ctx) >= 0.9) { const gchar *tmp; - guint i; for (i = 0; i < priv->locations->len; i++) { tmp = g_ptr_array_index (priv->locations, i); as_node_insert (n, "location", tmp, @@ -473,6 +517,18 @@ as_release_node_insert (AsRelease *release, GNode *parent, AsNodeContext *ctx) AS_NODE_INSERT_FLAG_PRE_ESCAPED | AS_NODE_INSERT_FLAG_DEDUPE_LANG); } + + /* add sizes */ + for (i = 0; i < AS_SIZE_KIND_LAST; i++) { + g_autofree gchar *size_str = NULL; + if (priv->size[i] == 0) + continue; + size_str = g_strdup_printf ("%" G_GUINT64_FORMAT, priv->size[i]); + as_node_insert (n, "size", size_str, + AS_NODE_INSERT_FLAG_NONE, + "type", as_size_kind_to_string (i), + NULL); + } return n; } @@ -526,6 +582,23 @@ as_release_node_parse (AsRelease *release, GNode *node, as_release_add_checksum (release, csum); } + /* get optional sizes */ + for (n = node->children; n != NULL; n = n->next) { + AsSizeKind kind; + if (as_node_get_tag (n) != AS_TAG_SIZE) + continue; + tmp = as_node_get_attribute (n, "type"); + if (tmp == NULL) + continue; + kind = as_size_kind_from_string (tmp); + if (kind == AS_SIZE_KIND_UNKNOWN) + continue; + tmp = as_node_get_data (n); + if (tmp == NULL) + continue; + priv->size[kind] = g_ascii_strtoull (tmp, NULL, 10); + } + /* AppStream: multiple <description> tags */ if (as_node_context_get_source_kind (ctx) == AS_APP_SOURCE_KIND_APPSTREAM) { for (n = node->children; n != NULL; n = n->next) { diff --git a/libappstream-glib/as-release.h b/libappstream-glib/as-release.h index 3f40aeb..4c7124b 100644 --- a/libappstream-glib/as-release.h +++ b/libappstream-glib/as-release.h @@ -66,6 +66,8 @@ AsChecksum *as_release_get_checksum_by_target (AsRelease *release, AsChecksumTarget target); GPtrArray *as_release_get_checksums (AsRelease *release); AsUrgencyKind as_release_get_urgency (AsRelease *release); +guint64 as_release_get_size (AsRelease *release, + AsSizeKind kind); /* setters */ void as_release_set_version (AsRelease *release, @@ -83,6 +85,9 @@ void as_release_add_checksum (AsRelease *release, AsChecksum *checksum); void as_release_set_urgency (AsRelease *release, AsUrgencyKind urgency); +void as_release_set_size (AsRelease *release, + AsSizeKind kind, + guint64 size); G_END_DECLS diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 4aa5363..5e8b2ee 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -432,6 +432,7 @@ as_test_release_appstream_func (void) AsNode *root; GString *xml; gboolean ret; + guint64 sz; const gchar *src = "<release version=\"0.1.2\" timestamp=\"123\">\n" "<location>http://foo.com/bar.zip</location>\n" @@ -440,6 +441,8 @@ as_test_release_appstream_func (void) "<checksum filename=\"firmware.cab\" target=\"container\" type=\"md5\">deadbeef</checksum>\n" "<description><p>This is a new release</p><ul><li>Point</li></ul></description>\n" "<description xml:lang=\"pl\"><p>Oprogramowanie</p></description>\n" + "<size type=\"installed\">123456</size>\n" + "<size type=\"download\">654321</size>\n" "</release>\n"; g_autofree AsNodeContext *ctx = NULL; g_autoptr(AsRelease) release = NULL; @@ -485,6 +488,12 @@ as_test_release_appstream_func (void) g_assert (csum != NULL); g_assert_cmpstr (as_checksum_get_value (csum), ==, "12345"); + /* test size */ + sz = as_release_get_size (release, AS_SIZE_KIND_INSTALLED); + g_assert_cmpuint (sz, ==, 123456); + sz = as_release_get_size (release, AS_SIZE_KIND_DOWNLOAD); + g_assert_cmpuint (sz, ==, 654321); + /* back to node */ root = as_node_new (); as_node_context_set_version (ctx, 1.0); diff --git a/libappstream-glib/as-tag.c b/libappstream-glib/as-tag.c index 08c5af5..e9d927d 100644 --- a/libappstream-glib/as-tag.c +++ b/libappstream-glib/as-tag.c @@ -190,6 +190,7 @@ as_tag_to_string (AsTag tag) "permission", "location", "checksum", + "size", NULL }; if (tag > AS_TAG_LAST) tag = AS_TAG_LAST; diff --git a/libappstream-glib/as-tag.gperf b/libappstream-glib/as-tag.gperf index 2add710..0c5ef64 100644 --- a/libappstream-glib/as-tag.gperf +++ b/libappstream-glib/as-tag.gperf @@ -54,3 +54,4 @@ permissions, AS_TAG_PERMISSIONS permission, AS_TAG_PERMISSION location, AS_TAG_LOCATION checksum, AS_TAG_CHECKSUM +size, AS_TAG_SIZE diff --git a/libappstream-glib/as-tag.h b/libappstream-glib/as-tag.h index 27eaa22..49aa56c 100644 --- a/libappstream-glib/as-tag.h +++ b/libappstream-glib/as-tag.h @@ -77,6 +77,7 @@ * @AS_TAG_PERMISSION: `permission` * @AS_TAG_LOCATION: `location` * @AS_TAG_CHECKSUM: `checksum` + * @AS_TAG_SIZE: `size` * * The tag type. **/ @@ -128,6 +129,7 @@ typedef enum { AS_TAG_PERMISSION, /* Since: 0.3.5 */ AS_TAG_LOCATION, /* Since: 0.3.5 */ AS_TAG_CHECKSUM, /* Since: 0.3.5 */ + AS_TAG_SIZE, /* Since: 0.5.2 */ /*< private >*/ AS_TAG_LAST } AsTag; |