summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2017-12-14 14:10:44 +0000
committerRichard Hughes <richard@hughsie.com>2017-12-14 14:35:29 +0000
commit21d67d5089c7312a9ccadc88fca0291ee556a365 (patch)
tree97ae4045f256173727f486c7921903308ebc91d8
parentf7a064e4509d7f24d57d39a71ecde90292de9a3b (diff)
downloadappstream-glib-21d67d5089c7312a9ccadc88fca0291ee556a365.tar.gz
Add as_store_get_apps_by_provide()
More than one component in an AppStream file may define the same provide.
-rw-r--r--libappstream-glib/as-self-test.c14
-rw-r--r--libappstream-glib/as-store.c38
-rw-r--r--libappstream-glib/as-store.h3
3 files changed, 55 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index 9be843c..7b3b6df 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -3862,6 +3862,8 @@ as_test_store_provides_func (void)
gboolean ret;
g_autoptr(GError) error = NULL;
g_autoptr(AsStore) store = NULL;
+ g_autoptr(GPtrArray) apps1 = NULL;
+ g_autoptr(GPtrArray) apps2 = NULL;
/* create a store and add a single app */
store = as_store_new ();
@@ -3890,6 +3892,18 @@ as_test_store_provides_func (void)
AS_PROVIDE_KIND_FIRMWARE_FLASHED,
"beefdead");
g_assert (app == NULL);
+
+ /* arrays of apps */
+ apps1 = as_store_get_apps_by_provide (store,
+ AS_PROVIDE_KIND_FIRMWARE_FLASHED,
+ "deadbeef");
+ g_assert_cmpint (apps1->len, ==, 1);
+ app = g_ptr_array_index (apps1, 0);
+ g_assert_cmpstr (as_app_get_id (app), ==, "test.desktop");
+ apps2 = as_store_get_apps_by_provide (store,
+ AS_PROVIDE_KIND_FIRMWARE_FLASHED,
+ "beefdead");
+ g_assert_cmpint (apps2->len, ==, 0);
}
static void
diff --git a/libappstream-glib/as-store.c b/libappstream-glib/as-store.c
index 12dd8de..67d331d 100644
--- a/libappstream-glib/as-store.c
+++ b/libappstream-glib/as-store.c
@@ -667,6 +667,44 @@ as_store_get_app_by_provide (AsStore *store, AsProvideKind kind, const gchar *va
}
/**
+ * as_store_get_apps_by_provide:
+ * @store: a #AsStore instance.
+ * @kind: the #AsProvideKind
+ * @value: the provide value, e.g. "com.hughski.ColorHug2.firmware"
+ *
+ * Finds any applications in the store by something that they provides.
+ *
+ * Returns: (transfer container) (element-type AsApp): an array of applications
+ *
+ * Since: 0.7.5
+ **/
+GPtrArray *
+as_store_get_apps_by_provide (AsStore *store, AsProvideKind kind, const gchar *value)
+{
+ AsStorePrivate *priv = GET_PRIVATE (store);
+ GPtrArray *apps = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
+
+ 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 (guint i = 0; i < priv->array->len; i++) {
+ AsApp *app = g_ptr_array_index (priv->array, i);
+ GPtrArray *provides = as_app_get_provides (app);
+ for (guint j = 0; j < provides->len; j++) {
+ AsProvide *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;
+ g_ptr_array_add (apps, g_object_ref (app));
+ }
+ }
+ return apps;
+}
+
+/**
* as_store_get_app_by_id_ignore_prefix:
* @store: a #AsStore instance.
* @id: the application full ID.
diff --git a/libappstream-glib/as-store.h b/libappstream-glib/as-store.h
index 1faf1ad..e0ed107 100644
--- a/libappstream-glib/as-store.h
+++ b/libappstream-glib/as-store.h
@@ -214,6 +214,9 @@ AsApp *as_store_get_app_by_pkgnames (AsStore *store,
AsApp *as_store_get_app_by_provide (AsStore *store,
AsProvideKind kind,
const gchar *value);
+GPtrArray *as_store_get_apps_by_provide (AsStore *store,
+ AsProvideKind kind,
+ const gchar *value);
void as_store_add_app (AsStore *store,
AsApp *app);
void as_store_add_apps (AsStore *store,