[gnome-software/wip/mcrha/fedora-third-party-followup-half] gs-overview-page: Use GsFedoraThirdParty



commit cddd168b8a4ef9fe21abcfbac6058a02231b638c
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 22 08:28:05 2021 +0200

    gs-overview-page: Use GsFedoraThirdParty

 src/gs-overview-page.c | 142 +++++++++----------------------------------------
 1 file changed, 26 insertions(+), 116 deletions(-)
---
diff --git a/src/gs-overview-page.c b/src/gs-overview-page.c
index 09ec17921..9ba33fb2b 100644
--- a/src/gs-overview-page.c
+++ b/src/gs-overview-page.c
@@ -40,7 +40,7 @@ struct _GsOverviewPage
        gboolean                 loading_categories;
        gboolean                 empty;
        GHashTable              *category_hash;         /* id : GsCategory */
-       gchar                   *third_party_cmdtool;
+       GsFedoraThirdParty      *third_party;
        gboolean                 third_party_needs_question;
 
        GtkWidget               *infobar_third_party;
@@ -380,85 +380,21 @@ is_fedora (void)
        return FALSE;
 }
 
-static void
-fedora_third_party_call_thread (GTask *task,
-                               gpointer source_object,
-                               gpointer task_data,
-                               GCancellable *cancellable)
-{
-       GPtrArray *args = task_data;
-       g_autoptr(GError) error = NULL;
-       gint exit_status = -1;
-
-       g_return_if_fail (args != NULL);
-
-       if (g_spawn_sync (NULL, (gchar **) args->pdata, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, 
&exit_status, &error))
-               g_task_return_int (task, WEXITSTATUS (exit_status));
-       else
-               g_task_return_error (task, g_steal_pointer (&error));
-}
-
-static void
-fedora_third_party_call_async (GsOverviewPage *self,
-                              const gchar *args[],
-                              GAsyncReadyCallback callback,
-                              gpointer user_data)
-{
-       g_autoptr(GTask) task = NULL;
-       GPtrArray *args_array;
-
-       g_return_if_fail (self->third_party_cmdtool != NULL);
-
-       args_array = g_ptr_array_new_with_free_func (g_free);
-       for (gsize ii = 0; args[ii] != NULL; ii++) {
-               g_ptr_array_add (args_array, g_strdup (args[ii]));
-       }
-
-       /* NULL-terminated array */
-       g_ptr_array_add (args_array, NULL);
-
-       task = g_task_new (self, NULL, callback, user_data);
-       g_task_set_source_tag (task, fedora_third_party_call_async);
-       g_task_set_task_data (task, args_array, (GDestroyNotify) g_ptr_array_unref);
-       g_task_run_in_thread (task, fedora_third_party_call_thread);
-}
-
-static gboolean
-fedora_third_party_call_finish (GsOverviewPage *self,
-                               GAsyncResult *result,
-                               gint *out_exit_status,
-                               GError **error)
-{
-       gint exit_status;
-       GError *local_error = NULL;
-
-       exit_status = g_task_propagate_int (G_TASK (result), &local_error);
-       if (local_error) {
-               g_propagate_error (error, local_error);
-               return FALSE;
-       }
-
-       if (out_exit_status)
-               *out_exit_status = exit_status;
-
-       return TRUE;
-}
-
 static void
 fedora_third_party_query_done_cb (GObject *source_object,
                                  GAsyncResult *result,
                                  gpointer user_data)
 {
-       GsOverviewPage *self = GS_OVERVIEW_PAGE (source_object);
+       GsFedoraThirdPartyState state = GS_FEDORA_THIRD_PARTY_STATE_UNKNOWN;
+       g_autoptr(GsOverviewPage) self = user_data;
        g_autoptr(GError) error = NULL;
-       gint exit_status = -1;
 
-       if (!fedora_third_party_call_finish (self, result, &exit_status, &error)) {
-               g_warning ("Failed to query '%s': %s", self->third_party_cmdtool, error->message);
+       if (!gs_fedora_third_party_query_finish (GS_FEDORA_THIRD_PARTY (source_object), result, &state, 
&error)) {
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       return;
+               g_warning ("Failed to query 'fedora-third-party': %s", error->message);
        } else {
-               /* The number 2 means no choice made yet, thus ask the user.
-                  See https://pagure.io/fedora-third-party/blob/main/f/doc/fedora-third-party.1.md */
-               self->third_party_needs_question = exit_status == 2;
+               self->third_party_needs_question = state == GS_FEDORA_THIRD_PARTY_STATE_ASK;
        }
 
        refresh_third_party_repo (self);
@@ -467,25 +403,14 @@ fedora_third_party_query_done_cb (GObject *source_object,
 static void
 reload_third_party_repo (GsOverviewPage *self)
 {
-       const gchar *args[] = {
-               "", /* executable */
-               "query",
-               "--quiet",
-               NULL
-       };
-
        /* Fedora-specific functionality */
        if (!is_fedora ())
                return;
 
-       if (self->third_party_cmdtool == NULL)
-               self->third_party_cmdtool = g_find_program_in_path ("fedora-third-party");
-
-       if (self->third_party_cmdtool == NULL)
+       if (!gs_fedora_third_party_is_available (self->third_party))
                return;
 
-       args[0] = self->third_party_cmdtool;
-       fedora_third_party_call_async (self, args, fedora_third_party_query_done_cb, NULL);
+       gs_fedora_third_party_query (self->third_party, self->cancellable, fedora_third_party_query_done_cb, 
g_object_ref (self));
 }
 
 static void
@@ -493,11 +418,14 @@ fedora_third_party_enable_done_cb (GObject *source_object,
                                   GAsyncResult *result,
                                   gpointer user_data)
 {
-       GsOverviewPage *self = GS_OVERVIEW_PAGE (source_object);
+       g_autoptr(GsOverviewPage) self = user_data;
        g_autoptr(GError) error = NULL;
 
-       if (!fedora_third_party_call_finish (self, result, NULL, &error))
-               g_warning ("Failed to enable '%s': %s", self->third_party_cmdtool, error->message);
+       if (!gs_fedora_third_party_switch_finish (GS_FEDORA_THIRD_PARTY (source_object), result, &error)) {
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       return;
+               g_warning ("Failed to enable 'fedora-third-party': %s", error->message);
+       }
 
        refresh_third_party_repo (self);
 }
@@ -505,17 +433,7 @@ fedora_third_party_enable_done_cb (GObject *source_object,
 static void
 fedora_third_party_enable (GsOverviewPage *self)
 {
-       const gchar *args[] = {
-               "pkexec",
-               "", /* executable */
-               "enable",
-               NULL
-       };
-
-       g_return_if_fail (self->third_party_cmdtool != NULL);
-
-       args[1] = self->third_party_cmdtool;
-       fedora_third_party_call_async (self, args, fedora_third_party_enable_done_cb, NULL);
+       gs_fedora_third_party_switch (self->third_party, TRUE, FALSE, self->cancellable, 
fedora_third_party_enable_done_cb, g_object_ref (self));
 }
 
 static void
@@ -523,11 +441,14 @@ fedora_third_party_disable_done_cb (GObject *source_object,
                                    GAsyncResult *result,
                                    gpointer user_data)
 {
-       GsOverviewPage *self = GS_OVERVIEW_PAGE (source_object);
+       g_autoptr(GsOverviewPage) self = user_data;
        g_autoptr(GError) error = NULL;
 
-       if (!fedora_third_party_call_finish (self, result, NULL, &error))
-               g_warning ("Failed to disable fedora-third-party: %s", error->message);
+       if (!gs_fedora_third_party_opt_out_finish (GS_FEDORA_THIRD_PARTY (source_object), result, &error)) {
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       return;
+               g_warning ("Failed to disable 'fedora-third-party': %s", error->message);
+       }
 
        refresh_third_party_repo (self);
 }
@@ -535,19 +456,7 @@ fedora_third_party_disable_done_cb (GObject *source_object,
 static void
 fedora_third_party_disable (GsOverviewPage *self)
 {
-       /* fedora-third-party-opt-out is a single-purpose script that changes
-        * the third-party status from unset => disabled. It exists to allow
-        * a different pkexec configuration for opting-out and thus avoid
-        * admin users needing to authenticate to opt-out.
-        */
-       const gchar *args[] = {
-               "pkexec",
-               "/usr/lib/fedora-third-party/fedora-third-party-opt-out",
-               NULL
-       };
-       g_return_if_fail (self->third_party_cmdtool != NULL);
-
-       fedora_third_party_call_async (self, args, fedora_third_party_disable_done_cb, NULL);
+       gs_fedora_third_party_opt_out (self->third_party, self->cancellable, 
fedora_third_party_disable_done_cb, g_object_ref (self));
 }
 
 static void
@@ -685,6 +594,7 @@ gs_overview_page_setup (GsPage *page,
        g_return_val_if_fail (GS_IS_OVERVIEW_PAGE (self), TRUE);
 
        self->plugin_loader = g_object_ref (plugin_loader);
+       self->third_party = gs_fedora_third_party_new ();
        self->cancellable = g_object_ref (cancellable);
        self->category_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                     g_free, (GDestroyNotify) g_object_unref);
@@ -793,7 +703,7 @@ gs_overview_page_dispose (GObject *object)
 
        g_clear_object (&self->plugin_loader);
        g_clear_object (&self->cancellable);
-       g_clear_pointer (&self->third_party_cmdtool, g_free);
+       g_clear_object (&self->third_party);
        g_clear_pointer (&self->category_hash, g_hash_table_unref);
 
        G_OBJECT_CLASS (gs_overview_page_parent_class)->dispose (object);


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