[gnome-software/mwleeds/hardcoded-pwa-list: 14/18] Rework app ID handling




commit f500b124a347d80ae743cbc33ca707b52e16047c
Author: Phaedrus Leeds <mwleeds protonmail com>
Date:   Wed Mar 23 13:46:54 2022 -0700

    Rework app ID handling

 plugins/epiphany/gs-plugin-epiphany.c | 101 ++++++++++++++++++----------------
 1 file changed, 55 insertions(+), 46 deletions(-)
---
diff --git a/plugins/epiphany/gs-plugin-epiphany.c b/plugins/epiphany/gs-plugin-epiphany.c
index 336d9cd2b..be7978ece 100644
--- a/plugins/epiphany/gs-plugin-epiphany.c
+++ b/plugins/epiphany/gs-plugin-epiphany.c
@@ -95,6 +95,7 @@ gs_plugin_epiphany_changed_cb (GFileMonitor      *monitor,
        GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (user_data);
 
        gs_plugin_cache_invalidate (GS_PLUGIN (self));
+       g_hash_table_remove_all (self->url_id_map);
 
        /* With the current API this is the only way to reload the list of
         * installed apps.
@@ -128,10 +129,9 @@ gs_plugin_epiphany_setup_async (GsPlugin            *plugin,
        if (connection)
                self->connection = g_object_ref (connection);
 
-       /* This is a mapping from URL to app ID, where the app ID comes from an
-        * appstream file. This allows us to use the AppStream app ID rather
-        * than the one provided by Epiphany for GsApp objects of installed
-        * apps.
+       /* This is a mapping from URL to app ID, where the app ID comes from
+        * Epiphany. This allows us to use that app ID rather than the
+        * AppStream app ID in certain contexts.
         */
        self->url_id_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
@@ -416,11 +416,13 @@ gs_plugin_epiphany_list_installed_apps_async (GsPlugin                       *pl
 
 /* Run in @worker */
 static void
-refine_app (GsApp      *app,
-           GUri       *uri,
-           const char *url)
+refine_app (GsPluginEpiphany *self,
+           GsApp            *app,
+           GUri             *uri,
+           const char       *url)
 {
        const char *hostname;
+       const char *installed_app_id;
        hostname = g_uri_get_host (uri);
 
        g_return_if_fail (GS_IS_APP (app));
@@ -432,6 +434,11 @@ refine_app (GsApp      *app,
 
        gs_app_set_scope (app, AS_COMPONENT_SCOPE_USER);
 
+       installed_app_id = g_hash_table_lookup (self->url_id_map, url);
+       if (installed_app_id) {
+               gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID, installed_app_id);
+       }
+
        /* 1 is a special value meaning 0 */
        gs_app_set_size_download (app, 1);
 
@@ -530,7 +537,6 @@ list_installed_apps_thread_cb (GTask        *task,
                const gchar *desktop_path;
                const gchar *name;
                const gchar *url = NULL;
-               const gchar *metainfo_app_id;
                g_autofree char *icon_path = NULL;
                const gchar *exec;
                int argc;
@@ -582,27 +588,19 @@ list_installed_apps_thread_cb (GTask        *task,
                        desktop_size = g_file_info_get_size (file_info);
                }
 
-               /* Create the GsApp using the app id from the appstream
-                * metainfo, but store the app id provided by epiphany for
-                * later use
-                */
-               metainfo_app_id = g_hash_table_lookup (self->url_id_map, url);
-               if (metainfo_app_id) {
-                       g_debug ("Creating GsApp for webapp with URL %s using app ID from metainfo: %s"
-                                " (Epiphany app ID: %s)", url, metainfo_app_id, desktop_file_id);
-                       app = gs_epiphany_create_app (self, metainfo_app_id);
-               } else {
-                       g_debug ("Creating GsApp for webapp with URL %s using app ID from Epiphany: %s",
-                                url, desktop_file_id);
-                       app = gs_epiphany_create_app (self, desktop_file_id);
-               }
-               gs_app_set_metadata (app, "epiphany::installed-app-id", desktop_file_id);
+               /* Store the installed app id for use in refine_app() */
+               g_hash_table_insert (self->url_id_map, g_strdup (url),
+                                    g_strdup (desktop_file_id));
+
+               g_debug ("Creating GsApp for webapp with URL %s using app ID from Epiphany: %s",
+                        url, desktop_file_id);
+               app = gs_epiphany_create_app (self, desktop_file_id);
 
                gs_app_set_state (app, GS_APP_STATE_INSTALLED);
                gs_app_set_name (app, GS_APP_QUALITY_NORMAL, name);
                gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_URL, url);
 
-               refine_app (app, uri, url);
+               refine_app (self, app, uri, url);
 
                if (icon_path) {
                        g_autoptr(GFile) icon_file = g_file_new_for_path (icon_path);
@@ -652,16 +650,18 @@ list_installed_apps_thread_cb (GTask        *task,
        gs_plugin_cache_lookup_by_state (GS_PLUGIN (self), installed_cache, GS_APP_STATE_INSTALLED);
        for (guint i = 0; i < gs_app_list_length (installed_cache); i++) {
                GsApp *app = gs_app_list_index (installed_cache, i);
-               g_autoptr(GsApp) app_cached = NULL;
                const char *installed_app_id;
-               const char *app_id;
 
-               installed_app_id = gs_app_get_metadata_item (app, "epiphany::installed-app-id");
-               app_id = installed_app_id ? installed_app_id : gs_app_get_id (app);
+               installed_app_id = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID);
+               if (installed_app_id == NULL) {
+                       g_warning ("Installed app unexpectedly has no desktop id: %s", gs_app_get_id (app));
+                       continue;
+               }
 
-               if (g_strv_contains ((const char * const *)webapps, app_id))
+               if (g_strv_contains ((const char * const *)webapps, installed_app_id))
                        continue;
 
+               /* The app may not be available if it was installed via Epiphany */
                gs_app_set_state (app, GS_APP_STATE_UNKNOWN);
                gs_plugin_cache_remove (GS_PLUGIN (self), gs_app_get_id (app));
        }
@@ -679,8 +679,9 @@ gs_plugin_epiphany_list_installed_apps_finish (GsPlugin      *plugin,
 }
 
 static void
-gs_epiphany_refine_app (GsApp      *app,
-                       const char *url)
+gs_epiphany_refine_app (GsPluginEpiphany *self,
+                       GsApp            *app,
+                       const char       *url)
 {
        g_autoptr(GUri) uri = NULL;
 
@@ -691,7 +692,7 @@ gs_epiphany_refine_app (GsApp      *app,
                return;
        }
 
-       refine_app (app, uri, url);
+       refine_app (self, app, uri, url);
 }
 
 static void refine_thread_cb (GTask        *task,
@@ -735,7 +736,6 @@ refine_thread_cb (GTask        *task,
        for (guint i = 0; i < gs_app_list_length (list); i++) {
                GsApp *app = gs_app_list_index (list, i);
                const char *url;
-               const char *source_plugin;
 
                /* not us */
                if (gs_app_get_kind (app) != AS_COMPONENT_KIND_WEB_APP ||
@@ -750,14 +750,7 @@ refine_thread_cb (GTask        *task,
                }
 
                g_debug ("epiphany: refining app %s", gs_app_get_id (app));
-               source_plugin = gs_app_get_metadata_item (app, "GnomeSoftware::Creator");
-               if (g_strcmp0 (source_plugin, "appstream") == 0) {
-                       g_debug ("epiphany: mapping url %s to app %s", url, gs_app_get_id (app));
-                       g_hash_table_insert (self->url_id_map, g_strdup (url),
-                                            g_strdup (gs_app_get_id (app)));
-               }
-
-               gs_epiphany_refine_app (app, url);
+               gs_epiphany_refine_app (self, app, url);
                gs_epiphany_refine_app_state (GS_PLUGIN (self), app);
 
                /* Usually the way to refine wildcard apps is to create a new
@@ -916,6 +909,9 @@ gs_plugin_app_install (GsPlugin      *plugin,
                gs_app_set_state_recover (app);
                return FALSE;
        }
+
+       g_hash_table_insert (self->url_id_map, g_strdup (url),
+                            g_strdup (installed_desktop_id));
        gs_app_set_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID, installed_desktop_id);
        gs_app_set_state (app, GS_APP_STATE_INSTALLED);
 
@@ -930,24 +926,37 @@ gs_plugin_app_remove (GsPlugin      *plugin,
 {
        GsPluginEpiphany *self = GS_PLUGIN_EPIPHANY (plugin);
        const char *installed_app_id;
-       const char *app_id;
+       const char *url;
 
        if (!gs_app_has_management_plugin (app, plugin))
                return TRUE;
 
-       installed_app_id = gs_app_get_metadata_item (app, "epiphany::installed-app-id");
-       app_id = installed_app_id ? installed_app_id : gs_app_get_id (app);
+       installed_app_id = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_DESKTOP_ID);
+       if (installed_app_id == NULL) {
+               g_set_error_literal (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_NOT_SUPPORTED,
+                                    "App can't be uninstalled without installed app ID");
+               gs_app_set_state_recover (app);
+               return FALSE;
+       }
 
        gs_app_set_state (app, GS_APP_STATE_REMOVING);
        if (!gs_ephy_web_app_provider_call_uninstall_sync (self->epiphany_proxy,
-                                                          app_id,
+                                                          installed_app_id,
                                                           cancellable,
                                                           error)) {
                gs_epiphany_error_convert (error);
                gs_app_set_state_recover (app);
                return FALSE;
        }
-       gs_app_set_state (app, GS_APP_STATE_AVAILABLE);
+
+       url = gs_app_get_launchable (app, AS_LAUNCHABLE_KIND_URL);
+       if (url != NULL && *url != '\0')
+               g_hash_table_remove (self->url_id_map, url);
+
+       /* The app may not be available if it was installed via Epiphany */
+       gs_app_set_state (app, GS_APP_STATE_UNKNOWN);
 
        return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]