[gnome-software/wip/mcrha/fedora-third-party-followup] gs-repos-dialog: Use GsFedoraThirdParty



commit 1d04b10862aaaa9ee073ea74950789532ab8faeb
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 22 08:30:13 2021 +0200

    gs-repos-dialog: Use GsFedoraThirdParty

 src/gs-repos-dialog.c | 228 +++++++++-----------------------------------------
 1 file changed, 41 insertions(+), 187 deletions(-)
---
diff --git a/src/gs-repos-dialog.c b/src/gs-repos-dialog.c
index 98aa0f208..bb3f77b51 100644
--- a/src/gs-repos-dialog.c
+++ b/src/gs-repos-dialog.c
@@ -24,7 +24,7 @@ struct _GsReposDialog
 {
        HdyWindow        parent_instance;
        GSettings       *settings;
-       gchar           *third_party_cmdtool;
+       GsFedoraThirdParty *third_party;
        gboolean         third_party_enabled;
        GHashTable      *third_party_repos; /* (nullable) (owned), mapping from owned repo ID → owned plugin 
name */
        GHashTable      *sections; /* gchar * ~> GsReposSection * */
@@ -329,80 +329,19 @@ repo_section_remove_clicked_cb (GsReposSection *section,
        remove_confirm_repo (dialog, row, repo, GS_PLUGIN_ACTION_REMOVE_REPO);
 }
 
-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 (GsReposDialog *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 (GsReposDialog *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_switch_done_cb (GObject *source_object,
                                   GAsyncResult *result,
                                   gpointer user_data)
 {
-       GsReposDialog *self = GS_REPOS_DIALOG (source_object);
+       g_autoptr(GsReposDialog) self = user_data;
        g_autoptr(GError) error = NULL;
 
-       if (!fedora_third_party_call_finish (self, result, NULL, &error))
-               g_warning ("Failed to switch config with '%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 switch 'fedora-third-party' config: %s", error->message);
+       }
 
        /* Reload the state, because the user could dismiss the authentication prompt
           or the repos could change their state. */
@@ -415,40 +354,23 @@ fedora_third_party_repos_switch_notify_cb (GObject *object,
                                           gpointer user_data)
 {
        GsReposDialog *self = user_data;
-       const gchar *args[] = {
-               "pkexec",
-               "", /* executable */
-               "", /* command */
-               "--config-only",
-               NULL
-       };
-
-       g_return_if_fail (self->third_party_cmdtool != NULL);
-
-       args[1] = self->third_party_cmdtool;
-       args[2] = gtk_switch_get_active (GTK_SWITCH (object)) ? "enable" : "disable";
 
-       fedora_third_party_call_async (self, args, fedora_third_party_switch_done_cb, NULL);
+       gs_fedora_third_party_switch (self->third_party,
+                                     gtk_switch_get_active (GTK_SWITCH (object)),
+                                     TRUE,
+                                     self->cancellable,
+                                     fedora_third_party_switch_done_cb,
+                                     g_object_ref (self));
 }
 
 static gboolean
 is_third_party_repo (GsReposDialog *dialog,
                     GsApp *repo)
 {
-       if (dialog->third_party_repos != NULL &&
-           gs_app_get_scope (repo) == AS_COMPONENT_SCOPE_SYSTEM) {
-               const gchar *expected_management_plugin;
-               const gchar *management_plugin;
-
-               expected_management_plugin = g_hash_table_lookup (dialog->third_party_repos, gs_app_get_id 
(repo));
-               if (expected_management_plugin == NULL)
-                       return FALSE;
-
-               management_plugin = gs_app_get_management_plugin (repo);
-               return g_strcmp0 (management_plugin, expected_management_plugin) == 0;
-       }
-
-       return FALSE;
+       return gs_app_get_scope (repo) == AS_COMPONENT_SCOPE_SYSTEM &&
+              gs_fedora_third_party_util_is_third_party_repo (dialog->third_party_repos,
+                                                              gs_app_get_id (repo),
+                                                              gs_app_get_management_plugin (repo));
 }
 
 static void
@@ -660,59 +582,22 @@ reload_sources (GsReposDialog *dialog)
                                            dialog);
 }
 
-static void
-fedora_third_party_list_repos_thread (GTask *task,
-                                     gpointer source_object,
-                                     gpointer task_data,
-                                     GCancellable *cancellable)
-{
-       GPtrArray *args = task_data;
-       g_autoptr(GError) error = NULL;
-       g_autofree gchar *stdoutput = NULL;
-
-       g_return_if_fail (args != NULL);
-
-       if (g_spawn_sync (NULL, (gchar **) args->pdata, NULL, G_SPAWN_DEFAULT, NULL, NULL, &stdoutput, NULL, 
NULL, &error)) {
-               GHashTable *repos = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-               g_auto(GStrv) lines = NULL;
-
-               lines = g_strsplit (stdoutput != NULL ? stdoutput : "", "\n", -1);
-
-               for (gsize ii = 0; lines != NULL && lines[ii]; ii++) {
-                       g_auto(GStrv) tokens = g_strsplit (lines[ii], ",", 2);
-                       if (tokens != NULL && tokens[0] != NULL && tokens[1] != NULL) {
-                               const gchar *repo_type = tokens[0];
-                               /* The 'dnf' means 'packagekit' here */
-                               if (g_str_equal (repo_type, "dnf"))
-                                       repo_type = "packagekit";
-                               /* Hash them by name, which cannot clash between types */
-                               g_hash_table_insert (repos, g_strdup (tokens[1]), g_strdup (repo_type));
-                       }
-               }
-
-               if (g_hash_table_size (repos) == 0)
-                       g_clear_pointer (&repos, g_hash_table_unref);
-
-               g_task_return_pointer (task, repos, (GDestroyNotify) g_hash_table_unref);
-       } else
-               g_task_return_error (task, g_steal_pointer (&error));
-}
-
 static void
 fedora_third_party_list_repos_done_cb (GObject *source_object,
                                       GAsyncResult *result,
                                       gpointer user_data)
 {
-       GsReposDialog *self = GS_REPOS_DIALOG (source_object);
+       g_autoptr(GsReposDialog) self = user_data;
        g_autoptr(GHashTable) repos = NULL;
        g_autoptr(GError) error = NULL;
 
-       repos = g_task_propagate_pointer (G_TASK (result), &error);
-
-       if (error)
-               g_warning ("Failed to list repos '%s': %s", self->third_party_cmdtool, error->message);
-       else
+       if (!gs_fedora_third_party_list_finish (GS_FEDORA_THIRD_PARTY (source_object), result, &repos, 
&error)) {
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       return;
+               g_warning ("Failed to list 'fedora-third-party' repos: %s", error->message);
+       } else {
                self->third_party_repos = g_steal_pointer (&repos);
+       }
 
        reload_sources (self);
 }
@@ -722,43 +607,20 @@ fedora_third_party_query_done_cb (GObject *source_object,
                                  GAsyncResult *result,
                                  gpointer user_data)
 {
-       GsReposDialog *self = GS_REPOS_DIALOG (source_object);
-       g_autoptr(GTask) task = NULL;
+       GsFedoraThirdPartyState state = GS_FEDORA_THIRD_PARTY_STATE_UNKNOWN;
+       g_autoptr(GsReposDialog) self = user_data;
        g_autoptr(GError) error = NULL;
-       gint exit_status;
-       GPtrArray *args_array;
-       const gchar *args[] = {
-               "", /* executable */
-               "list",
-               "--csv",
-               "--columns=type,name",
-               NULL
-       };
-
-       if (!fedora_third_party_call_finish (self, result, &exit_status, &error)) {
-               g_warning ("Failed to query '%s': %s", self->third_party_cmdtool, error->message);
-       } else {
-               /* The number 0 means it's enabled.
-                  See https://pagure.io/fedora-third-party/blob/main/f/doc/fedora-third-party.1.md */
-               self->third_party_enabled = exit_status == 0;
-       }
 
-       g_return_if_fail (self->third_party_cmdtool != NULL);
-
-       args[0] = self->third_party_cmdtool;
-
-       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]));
+       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 {
+               self->third_party_enabled = state == GS_FEDORA_THIRD_PARTY_STATE_ENABLED;
        }
 
-       /* NULL-terminated array */
-       g_ptr_array_add (args_array, NULL);
-
-       task = g_task_new (self, NULL, fedora_third_party_list_repos_done_cb, NULL);
-       g_task_set_source_tag (task, fedora_third_party_query_done_cb);
-       g_task_set_task_data (task, args_array, (GDestroyNotify) g_ptr_array_unref);
-       g_task_run_in_thread (task, fedora_third_party_list_repos_thread);
+       gs_fedora_third_party_list (self->third_party, self->cancellable,
+                                   fedora_third_party_list_repos_done_cb, g_object_ref (self));
 }
 
 static gboolean
@@ -781,31 +643,22 @@ is_fedora (void)
 static void
 reload_third_party_repos (GsReposDialog *dialog)
 {
-       const gchar *args[] = {
-               "", /* executable */
-               "query",
-               "--quiet",
-               NULL
-       };
-
        /* Fedora-specific functionality */
        if (!is_fedora ()) {
                reload_sources (dialog);
                return;
        }
 
-       g_clear_pointer (&dialog->third_party_repos, g_hash_table_unref);
-       g_clear_pointer (&dialog->third_party_cmdtool, g_free);
+       gs_fedora_third_party_invalidate (dialog->third_party);
 
-       dialog->third_party_cmdtool = g_find_program_in_path ("fedora-third-party");
-
-       if (dialog->third_party_cmdtool == NULL) {
+       if (!gs_fedora_third_party_is_available (dialog->third_party)) {
                reload_sources (dialog);
                return;
        }
 
-       args[0] = dialog->third_party_cmdtool;
-       fedora_third_party_call_async (dialog, args, fedora_third_party_query_done_cb, NULL);
+       g_clear_pointer (&dialog->third_party_repos, g_hash_table_unref);
+
+       gs_fedora_third_party_query (dialog->third_party, dialog->cancellable, 
fedora_third_party_query_done_cb, g_object_ref (dialog));
 }
 
 static gchar *
@@ -851,9 +704,9 @@ gs_repos_dialog_dispose (GObject *object)
        }
 
        g_cancellable_cancel (dialog->cancellable);
-       g_clear_pointer (&dialog->third_party_cmdtool, g_free);
        g_clear_pointer (&dialog->third_party_repos, g_hash_table_unref);
        g_clear_pointer (&dialog->sections, g_hash_table_unref);
+       g_clear_object (&dialog->third_party);
        g_clear_object (&dialog->cancellable);
        g_clear_object (&dialog->settings);
 
@@ -869,6 +722,7 @@ gs_repos_dialog_init (GsReposDialog *dialog)
 
        gtk_widget_init_template (GTK_WIDGET (dialog));
 
+       dialog->third_party = gs_fedora_third_party_new ();
        dialog->cancellable = g_cancellable_new ();
        dialog->settings = g_settings_new ("org.gnome.software");
        dialog->sections = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);


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