summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2015-08-10 18:05:45 +0100
committerRichard Hughes <richard@hughsie.com>2015-08-10 18:06:11 +0100
commiteee40b238adf84b77ac4d2f4e1f585a291c4299d (patch)
treed726cddaca2fbbd343acf5d1ff1f5f1f53e85ec3
parent93f1a4b5ab2e6d80d66027cd80edef4dabe45b24 (diff)
downloadappstream-glib-eee40b238adf84b77ac4d2f4e1f585a291c4299d.tar.gz
Find the application in a store by the provide value
-rw-r--r--libappstream-glib/as-self-test.c38
-rw-r--r--libappstream-glib/as-store.c43
-rw-r--r--libappstream-glib/as-store.h3
3 files changed, 84 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index cb6020c..909f6db 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -2902,6 +2902,43 @@ as_test_store_func (void)
}
static void
+as_test_store_provides_func (void)
+{
+ AsApp *app;
+ gboolean ret;
+ _cleanup_error_free_ GError *error = NULL;
+ _cleanup_object_unref_ AsStore *store = NULL;
+
+ /* create a store and add a single app */
+ store = as_store_new ();
+ ret = as_store_from_xml (store,
+ "<components version=\"0.6\">"
+ "<component type=\"desktop\">"
+ "<id>test.desktop</id>"
+ "<provides>"
+ "<firmware type=\"flashed\">deadbeef</firmware>"
+ "</provides>"
+ "</component>"
+ "</components>", NULL, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+
+ /* get an appication by the provide value */
+ app = as_store_get_app_by_provide (store,
+ AS_PROVIDE_KIND_FIRMWARE_FLASHED,
+ "deadbeef");
+ g_assert_cmpstr (as_app_get_id (app), ==, "test.desktop");
+ app = as_store_get_app_by_provide (store,
+ AS_PROVIDE_KIND_FIRMWARE_RUNTIME,
+ "deadbeef");
+ g_assert (app == NULL);
+ app = as_store_get_app_by_provide (store,
+ AS_PROVIDE_KIND_FIRMWARE_FLASHED,
+ "beefdead");
+ g_assert (app == NULL);
+}
+
+static void
as_test_store_versions_func (void)
{
AsApp *app;
@@ -4240,6 +4277,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/store{metadata-index}", as_test_store_metadata_index_func);
g_test_add_func ("/AppStream/store{validate}", as_test_store_validate_func);
g_test_add_func ("/AppStream/store{embedded}", as_test_store_embedded_func);
+ g_test_add_func ("/AppStream/store{provides}", as_test_store_provides_func);
g_test_add_func ("/AppStream/store{local-app-install}", as_test_store_local_app_install_func);
g_test_add_func ("/AppStream/store{local-appdata}", as_test_store_local_appdata_func);
g_test_add_func ("/AppStream/store{speed-appstream}", as_test_store_speed_appstream_func);
diff --git a/libappstream-glib/as-store.c b/libappstream-glib/as-store.c
index d9949bc..b2d7f91 100644
--- a/libappstream-glib/as-store.c
+++ b/libappstream-glib/as-store.c
@@ -429,6 +429,49 @@ as_store_get_app_by_id (AsStore *store, const gchar *id)
}
/**
+ * as_store_get_app_by_provide:
+ * @store: a #AsStore instance.
+ * @kind: the #AsProvideKind
+ * @value: the provide value, e.g. "com.hughski.ColorHug2.firmware"
+ *
+ * Finds an application in the store by something that it provides.
+ *
+ * Returns: (transfer none): a #AsApp or %NULL
+ *
+ * Since: 0.5.0
+ **/
+AsApp *
+as_store_get_app_by_provide (AsStore *store, AsProvideKind kind, const gchar *value)
+{
+ AsApp *app;
+ AsProvide *tmp;
+ AsStorePrivate *priv = GET_PRIVATE (store);
+ guint i;
+ guint j;
+ GPtrArray *provides;
+
+ g_return_val_if_fail (AS_IS_STORE (store), NULL);
+ g_return_val_if_fail (kind != AS_PROVIDE_KIND_UNKNOWN, NULL);
+ g_return_val_if_fail (value != NULL, NULL);
+
+ /* find an application that provides something */
+ for (i = 0; i < priv->array->len; i++) {
+ app = g_ptr_array_index (priv->array, i);
+ provides = as_app_get_provides (app);
+ for (j = 0; j < provides->len; j++) {
+ tmp = g_ptr_array_index (provides, j);
+ if (kind != as_provide_get_kind (tmp))
+ continue;
+ if (g_strcmp0 (as_provide_get_value (tmp), value) != 0)
+ continue;
+ return app;
+ }
+ }
+ return NULL;
+
+}
+
+/**
* as_store_get_app_by_id_with_fallbacks:
* @store: a #AsStore instance.
* @id: the application full ID.
diff --git a/libappstream-glib/as-store.h b/libappstream-glib/as-store.h
index ab46d94..3ca2ffc 100644
--- a/libappstream-glib/as-store.h
+++ b/libappstream-glib/as-store.h
@@ -169,6 +169,9 @@ AsApp *as_store_get_app_by_pkgname (AsStore *store,
const gchar *pkgname);
AsApp *as_store_get_app_by_pkgnames (AsStore *store,
gchar **pkgnames);
+AsApp *as_store_get_app_by_provide (AsStore *store,
+ AsProvideKind kind,
+ const gchar *value);
void as_store_add_app (AsStore *store,
AsApp *app);
void as_store_remove_app (AsStore *store,