summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-04-07 12:27:14 +0100
committerRichard Hughes <richard@hughsie.com>2016-04-07 12:27:45 +0100
commit75f7bd8f010ea109e6d2c177bc14d0ce005b6b25 (patch)
treecdb670acabc4593fcf5f1592e55c672497f564be
parent23f5c271ae2188ae25517ff15632ee44a74efc6c (diff)
downloadappstream-glib-75f7bd8f010ea109e6d2c177bc14d0ce005b6b25.tar.gz
Fall back to searching in as_store_get_app_by_pkgname()
If the user does: - as_store_load() - as_store_get_app_by_id() - as_app_add_pkgname() - as_store_get_app_by_pkgname() Then we don't return the app. This is probably not what the user expects.
-rw-r--r--libappstream-glib/as-store.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/libappstream-glib/as-store.c b/libappstream-glib/as-store.c
index df9ce01..82dbd4a 100644
--- a/libappstream-glib/as-store.c
+++ b/libappstream-glib/as-store.c
@@ -667,6 +667,24 @@ as_store_get_app_by_id_with_fallbacks (AsStore *store, const gchar *id)
}
/**
+ * as_app_has_pkgname:
+ **/
+static gboolean
+as_app_has_pkgname (AsApp *app, const gchar *pkgname)
+{
+ guint i;
+ GPtrArray *pkgnames;
+
+ pkgnames = as_app_get_pkgnames (app);
+ for (i = 0; i < pkgnames->len; i++) {
+ const gchar *tmp = g_ptr_array_index (pkgnames, i);
+ if (g_strcmp0 (tmp, pkgname) == 0)
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
* as_store_get_app_by_pkgname:
* @store: a #AsStore instance.
* @pkgname: the package name.
@@ -680,9 +698,27 @@ as_store_get_app_by_id_with_fallbacks (AsStore *store, const gchar *id)
AsApp *
as_store_get_app_by_pkgname (AsStore *store, const gchar *pkgname)
{
+ AsApp *app;
AsStorePrivate *priv = GET_PRIVATE (store);
+ guint i;
+
g_return_val_if_fail (AS_IS_STORE (store), NULL);
- return g_hash_table_lookup (priv->hash_pkgname, pkgname);
+
+ /* in most cases, we can use the cache */
+ app = g_hash_table_lookup (priv->hash_pkgname, pkgname);
+ if (app != NULL)
+ return app;
+
+ /* fall back in case the user adds to app to the store, *then*
+ * uses as_app_add_pkgname() on the app */
+ for (i = 0; i < priv->array->len; i++) {
+ app = g_ptr_array_index (priv->array, i);
+ if (as_app_has_pkgname (app, pkgname))
+ return app;
+ }
+
+ /* not found */
+ return NULL;
}
/**