[gnome-software] Refactor authentication code to be common.



commit 2c4e80f809b08a98126c3ebb74b0e8f5eb344ec8
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Feb 2 16:44:54 2018 +1300

    Refactor authentication code to be common.

 src/gs-details-page.c |  29 ++++-------
 src/gs-page.c         | 141 +++++++++++++++++++++++++++-----------------------
 src/gs-page.h         |   8 +++
 3 files changed, 94 insertions(+), 84 deletions(-)
---
diff --git a/src/gs-details-page.c b/src/gs-details-page.c
index 2ac7b346..1669a0c8 100644
--- a/src/gs-details-page.c
+++ b/src/gs-details-page.c
@@ -1200,17 +1200,14 @@ gs_details_page_app_set_review_cb (GObject *source,
                                    gpointer user_data);
 
 static void
-gs_details_page_authenticate_cb (GtkDialog *dialog,
-                                 GtkResponseType response_type,
+gs_details_page_authenticate_cb (GsPage *page,
+                                gboolean authenticated,
                                 gpointer user_data)
 {
        g_autoptr(GsDetailsPageReviewHelper) helper = (GsDetailsPageReviewHelper *) user_data;
        g_autoptr(GsPluginJob) plugin_job = NULL;
 
-       /* unmap the dialog */
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-
-       if (response_type != GTK_RESPONSE_OK)
+       if (!authenticated)
                return;
 
        plugin_job = gs_plugin_job_newv (helper->action,
@@ -1240,20 +1237,12 @@ gs_details_page_app_set_review_cb (GObject *source,
                if (g_error_matches (error,
                                     GS_PLUGIN_ERROR,
                                     GS_PLUGIN_ERROR_AUTH_REQUIRED)) {
-                       g_autoptr(GError) error_local = NULL;
-                       GtkWidget *dialog;
-                       dialog = gs_auth_dialog_new (helper->self->plugin_loader,
-                                                    helper->app,
-                                                    gs_utils_get_error_value (error),
-                                                    &error_local);
-                       if (dialog == NULL) {
-                               g_warning ("%s", error_local->message);
-                               return;
-                       }
-                       gs_shell_modal_dialog_present (helper->self->shell, GTK_DIALOG (dialog));
-                       g_signal_connect (dialog, "response",
-                                         G_CALLBACK (gs_details_page_authenticate_cb),
-                                         helper);
+                       gs_page_authenticate (GS_PAGE (helper->self),
+                                             helper->app,
+                                             gs_utils_get_error_value (error),
+                                             helper->self->cancellable,
+                                             gs_details_page_authenticate_cb,
+                                             helper);
                        g_steal_pointer (&helper);
                        return;
                }
diff --git a/src/gs-page.c b/src/gs-page.c
index 1549cc79..5df2a5dc 100644
--- a/src/gs-page.c
+++ b/src/gs-page.c
@@ -50,6 +50,8 @@ typedef struct {
        GsPluginAction   action;
        GsShellInteraction interaction;
        GsPrice         *price;
+       GsPageAuthCallback callback;
+       gpointer         callback_data;
 } GsPageHelper;
 
 static void
@@ -72,24 +74,66 @@ gs_page_helper_free (GsPageHelper *helper)
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GsPageHelper, gs_page_helper_free);
 
+static void
+gs_page_authenticate_cb (GtkDialog *dialog,
+                GtkResponseType response_type,
+                gpointer user_data)
+{
+       g_autoptr(GsPageHelper) helper = user_data;
+
+       /* unmap the dialog */
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+
+       helper->callback (helper->page, response_type == GTK_RESPONSE_OK, helper->callback_data);
+}
+
+void
+gs_page_authenticate (GsPage *page,
+                     GsApp *app,
+                     const gchar *provider_id,
+                     GCancellable *cancellable,
+                      GsPageAuthCallback callback,
+                      gpointer user_data)
+{
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
+       g_autoptr(GsPageHelper) helper = NULL;
+       GtkWidget *dialog;
+       g_autoptr(GError) error = NULL;
+
+       helper = g_slice_new0 (GsPageHelper);
+       helper->callback = callback;
+       helper->callback = user_data = user_data;
+
+       dialog = gs_auth_dialog_new (priv->plugin_loader,
+                                    app,
+                                    provider_id,
+                                    &error);
+       if (dialog == NULL) {
+               g_warning ("%s", error->message);
+               return;
+       }
+       gs_shell_modal_dialog_present (priv->shell, GTK_DIALOG (dialog));
+       g_signal_connect (dialog, "response",
+                         G_CALLBACK (gs_page_authenticate_cb),
+                         helper);
+       g_steal_pointer (&helper);
+}
+
 static void
 gs_page_app_installed_cb (GObject *source,
                           GAsyncResult *res,
                           gpointer user_data);
 
 static void
-gs_page_install_authenticate_cb (GtkDialog *dialog,
-                                GtkResponseType response_type,
+gs_page_install_authenticate_cb (GsPage *page,
+                                gboolean authenticated,
                                 gpointer user_data)
 {
        g_autoptr(GsPageHelper) helper = (GsPageHelper *) user_data;
-       GsPagePrivate *priv = gs_page_get_instance_private (helper->page);
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
        g_autoptr(GsPluginJob) plugin_job = NULL;
 
-       /* unmap the dialog */
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-
-       if (response_type != GTK_RESPONSE_OK)
+       if (!authenticated)
                return;
 
        plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_INSTALL,
@@ -109,18 +153,15 @@ gs_page_app_removed_cb (GObject *source,
                         gpointer user_data);
 
 static void
-gs_page_remove_authenticate_cb (GtkDialog *dialog,
-                               GtkResponseType response_type,
+gs_page_remove_authenticate_cb (GsPage *page,
+                               gboolean authenticated,
                                gpointer user_data)
 {
        g_autoptr(GsPageHelper) helper = (GsPageHelper *) user_data;
-       GsPagePrivate *priv = gs_page_get_instance_private (helper->page);
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
        g_autoptr(GsPluginJob) plugin_job = NULL;
 
-       /* unmap the dialog */
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-
-       if (response_type != GTK_RESPONSE_OK)
+       if (!authenticated)
                return;
 
        plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_REMOVE,
@@ -159,20 +200,12 @@ gs_page_app_installed_cb (GObject *source,
                if (g_error_matches (error,
                                     GS_PLUGIN_ERROR,
                                     GS_PLUGIN_ERROR_AUTH_REQUIRED)) {
-                       g_autoptr(GError) error_local = NULL;
-                       GtkWidget *dialog;
-                       dialog = gs_auth_dialog_new (priv->plugin_loader,
-                                                    helper->app,
-                                                    gs_utils_get_error_value (error),
-                                                    &error_local);
-                       if (dialog == NULL) {
-                               g_warning ("%s", error_local->message);
-                               return;
-                       }
-                       gs_shell_modal_dialog_present (priv->shell, GTK_DIALOG (dialog));
-                       g_signal_connect (dialog, "response",
-                                         G_CALLBACK (gs_page_install_authenticate_cb),
-                                         helper);
+                       gs_page_authenticate (page,
+                                             helper->app,
+                                             gs_utils_get_error_value (error),
+                                             helper->cancellable,
+                                             gs_page_install_authenticate_cb,
+                                             helper);
                        g_steal_pointer (&helper);
                        return;
                }
@@ -204,7 +237,6 @@ gs_page_app_removed_cb (GObject *source,
        g_autoptr(GsPageHelper) helper = (GsPageHelper *) user_data;
        GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source);
        GsPage *page = helper->page;
-       GsPagePrivate *priv = gs_page_get_instance_private (page);
        gboolean ret;
        g_autoptr(GError) error = NULL;
 
@@ -222,20 +254,12 @@ gs_page_app_removed_cb (GObject *source,
                if (g_error_matches (error,
                                     GS_PLUGIN_ERROR,
                                     GS_PLUGIN_ERROR_AUTH_REQUIRED)) {
-                       g_autoptr(GError) error_local = NULL;
-                       GtkWidget *dialog;
-                       dialog = gs_auth_dialog_new (priv->plugin_loader,
-                                                    helper->app,
-                                                    gs_utils_get_error_value (error),
-                                                    &error_local);
-                       if (dialog == NULL) {
-                               g_warning ("%s", error_local->message);
-                               return;
-                       }
-                       gs_shell_modal_dialog_present (priv->shell, GTK_DIALOG (dialog));
-                       g_signal_connect (dialog, "response",
-                                         G_CALLBACK (gs_page_remove_authenticate_cb),
-                                         helper);
+                       gs_page_authenticate (page,
+                                             helper->app,
+                                             gs_utils_get_error_value (error),
+                                             helper->cancellable,
+                                             gs_page_remove_authenticate_cb,
+                                             helper);
                        g_steal_pointer (&helper);
                        return;
                }
@@ -288,18 +312,15 @@ gs_page_app_purchased_cb (GObject *source,
                           gpointer user_data);
 
 static void
-gs_page_purchase_authenticate_cb (GtkDialog *dialog,
-                                 GtkResponseType response_type,
+gs_page_purchase_authenticate_cb (GsPage *page,
+                                 gboolean authenticated,
                                  gpointer user_data)
 {
        g_autoptr(GsPageHelper) helper = (GsPageHelper *) user_data;
-       GsPagePrivate *priv = gs_page_get_instance_private (helper->page);
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
        g_autoptr(GsPluginJob) plugin_job = NULL;
 
-       /* unmap the dialog */
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-
-       if (response_type != GTK_RESPONSE_OK)
+       if (!authenticated)
                return;
 
        plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_PURCHASE,
@@ -340,20 +361,12 @@ gs_page_app_purchased_cb (GObject *source,
                if (g_error_matches (error,
                                     GS_PLUGIN_ERROR,
                                     GS_PLUGIN_ERROR_AUTH_REQUIRED)) {
-                       g_autoptr(GError) error_local = NULL;
-                       GtkWidget *dialog;
-                       dialog = gs_auth_dialog_new (priv->plugin_loader,
-                                                    helper->app,
-                                                    gs_utils_get_error_value (error),
-                                                    &error_local);
-                       if (dialog == NULL) {
-                               g_warning ("%s", error_local->message);
-                               return;
-                       }
-                       gs_shell_modal_dialog_present (priv->shell, GTK_DIALOG (dialog));
-                       g_signal_connect (dialog, "response",
-                                         G_CALLBACK (gs_page_purchase_authenticate_cb),
-                                         helper);
+                       gs_page_authenticate (page,
+                                             helper->app,
+                                             gs_utils_get_error_value (error),
+                                             helper->cancellable,
+                                             gs_page_purchase_authenticate_cb,
+                                             helper);
                        g_steal_pointer (&helper);
                        return;
                }
diff --git a/src/gs-page.h b/src/gs-page.h
index ebd27922..0d243cee 100644
--- a/src/gs-page.h
+++ b/src/gs-page.h
@@ -50,6 +50,8 @@ struct _GsPageClass
                                                 GError         **error);
 };
 
+typedef void (*GsPageAuthCallback) (GsPage *page, gboolean authorized, gpointer user_data);
+
 GsPage         *gs_page_new                            (void);
 GtkWidget      *gs_page_get_header_start_widget        (GsPage         *page);
 void            gs_page_set_header_start_widget        (GsPage         *page,
@@ -57,6 +59,12 @@ void          gs_page_set_header_start_widget        (GsPage         *page,
 GtkWidget      *gs_page_get_header_end_widget          (GsPage         *page);
 void            gs_page_set_header_end_widget          (GsPage         *page,
                                                         GtkWidget      *widget);
+void            gs_page_authenticate                   (GsPage                 *page,
+                                                        GsApp                  *app,
+                                                        const gchar            *provider_id,
+                                                        GCancellable           *cancellable,
+                                                        GsPageAuthCallback      callback,
+                                                        gpointer                user_data);
 void            gs_page_install_app                    (GsPage                 *page,
                                                         GsApp                  *app,
                                                         GsShellInteraction     interaction,


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