[gnome-software/wip/rancell/paid] Rename buy to purchase, update UI when purchased



commit 41ef57149d61d9d480dede576ce29d7a200e3065
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Feb 23 15:58:24 2016 +1300

    Rename buy to purchase, update UI when purchased

 src/gs-app.c                  |   22 ++++++++++++--
 src/gs-page.c                 |   63 +++++++++++++++++++++++++++++++++++++++++
 src/gs-page.h                 |    2 +
 src/gs-plugin-loader.h        |    1 +
 src/gs-plugin.h               |    2 +-
 src/gs-shell-details.c        |    8 +++++
 src/gs-utils.c                |    5 +++
 src/plugins/gs-plugin-dummy.c |   12 ++++----
 8 files changed, 105 insertions(+), 10 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index 1fd06a1..1ae1356 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -117,6 +117,7 @@ enum {
        PROP_DESCRIPTION,
        PROP_PRICE,
        PROP_CURRENCY,
+       PROP_OWNED,
        PROP_RATING,
        PROP_KIND,
        PROP_STATE,
@@ -1740,6 +1741,7 @@ gs_app_set_owned (GsApp *app, gboolean owned)
 {
        g_return_if_fail (GS_IS_APP (app));
        app->owned = owned;
+       gs_app_queue_notify (app, "owned");
 }
 
 /**
@@ -2296,6 +2298,9 @@ gs_app_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *
        case PROP_CURRENCY:
                g_value_set_string (value, app->currency);
                break;
+       case PROP_OWNED:
+               g_value_set_boolean (value, app->owned);
+               break;
        case PROP_RATING:
                g_value_set_int (value, app->rating);
                break;
@@ -2353,6 +2358,9 @@ gs_app_set_property (GObject *object, guint prop_id, const GValue *value, GParam
        case PROP_CURRENCY:
                gs_app_set_currency (app, g_value_get_string (value));
                break;
+       case PROP_OWNED:
+               gs_app_set_owned (app, g_value_get_boolean (value));
+               break;
        case PROP_RATING:
                gs_app_set_rating (app, g_value_get_int (value));
                break;
@@ -2489,10 +2497,10 @@ gs_app_class_init (GsAppClass *klass)
        /**
         * GsApp:price:
         */
-       pspec = g_param_spec_uint ("rating", NULL, NULL,
+       pspec = g_param_spec_uint ("price", NULL, NULL,
                                   0, G_MAXUINT, 0,
                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-       g_object_class_install_property (object_class, PROP_RATING, pspec);
+       g_object_class_install_property (object_class, PROP_PRICE, pspec);
 
        /**
         * GsApp:currency:
@@ -2500,7 +2508,15 @@ gs_app_class_init (GsAppClass *klass)
        pspec = g_param_spec_string ("currency", NULL, NULL,
                                     NULL,
                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-       g_object_class_install_property (object_class, PROP_RATING, pspec);
+       g_object_class_install_property (object_class, PROP_CURRENCY, pspec);
+
+       /**
+        * GsApp:owned:
+        */
+       pspec = g_param_spec_boolean ("owned", NULL, NULL,
+                                     FALSE,
+                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+       g_object_class_install_property (object_class, PROP_OWNED, pspec);
 
        /**
         * GsApp:rating:
diff --git a/src/gs-page.c b/src/gs-page.c
index d414c06..d1c32f7 100644
--- a/src/gs-page.c
+++ b/src/gs-page.c
@@ -54,6 +54,44 @@ install_remove_data_free (InstallRemoveData *data)
 }
 
 static void
+gs_page_app_bought_cb (GObject *source,
+                       GAsyncResult *res,
+                       gpointer user_data)
+{
+       GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source);
+       InstallRemoveData *data = (InstallRemoveData *) user_data;
+       GsPage *page = data->page;
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+
+       ret = gs_plugin_loader_app_action_finish (plugin_loader,
+                                                 res,
+                                                 &error);
+       if (!ret) {
+               g_warning ("failed to purchase %s: %s",
+                          gs_app_get_id (data->app),
+                          error->message);
+               gs_app_notify_failed_modal (data->app,
+                                           gs_shell_get_window (priv->shell),
+                                           GS_PLUGIN_LOADER_ACTION_PURCHASE,
+                                           error);
+               goto out;
+       }
+
+       /* only show this if the window is not active */
+       if (gs_app_get_state (data->app) != AS_APP_STATE_QUEUED_FOR_INSTALL &&
+           !gs_shell_is_active (priv->shell))
+               gs_app_notify_installed (data->app);
+
+       if (GS_PAGE_GET_CLASS (page)->app_installed != NULL)
+               GS_PAGE_GET_CLASS (page)->app_installed (page, data->app);
+
+out:
+       install_remove_data_free (data);
+}
+
+static void
 gs_page_app_installed_cb (GObject *source,
                           GAsyncResult *res,
                           gpointer user_data)
@@ -123,6 +161,31 @@ out:
 }
 
 void
+gs_page_purchase_app (GsPage *page, GsApp *app)
+{
+       GsPagePrivate *priv = gs_page_get_instance_private (page);
+       InstallRemoveData *data;
+       GtkResponseType response;
+
+       /* probably non-free */
+       if (gs_app_get_state (app) == AS_APP_STATE_UNAVAILABLE) {
+               response = gs_app_notify_unavailable (app, gs_shell_get_window (priv->shell));
+               if (response != GTK_RESPONSE_OK)
+                       return;
+       }
+
+       data = g_slice_new0 (InstallRemoveData);
+       data->app = g_object_ref (app);
+       data->page = g_object_ref (page);
+       gs_plugin_loader_app_action_async (priv->plugin_loader,
+                                          app,
+                                          GS_PLUGIN_LOADER_ACTION_PURCHASE,
+                                          priv->cancellable,
+                                          gs_page_app_bought_cb,
+                                          data);
+}
+
+void
 gs_page_install_app (GsPage *page, GsApp *app)
 {
        GsPagePrivate *priv = gs_page_get_instance_private (page);
diff --git a/src/gs-page.h b/src/gs-page.h
index 0f34a88..43370c4 100644
--- a/src/gs-page.h
+++ b/src/gs-page.h
@@ -45,6 +45,8 @@ struct _GsPageClass
 };
 
 GsPage         *gs_page_new                            (void);
+void            gs_page_purchase_app                   (GsPage         *page,
+                                                        GsApp          *app);
 void            gs_page_install_app                    (GsPage         *page,
                                                         GsApp          *app);
 void            gs_page_remove_app                     (GsPage         *page,
diff --git a/src/gs-plugin-loader.h b/src/gs-plugin-loader.h
index 2365cb0..d4dc0a9 100644
--- a/src/gs-plugin-loader.h
+++ b/src/gs-plugin-loader.h
@@ -53,6 +53,7 @@ typedef enum
 } GsPluginLoaderError;
 
 typedef enum {
+       GS_PLUGIN_LOADER_ACTION_PURCHASE,
        GS_PLUGIN_LOADER_ACTION_INSTALL,
        GS_PLUGIN_LOADER_ACTION_REMOVE,
        GS_PLUGIN_LOADER_ACTION_UPDATE,
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index ef2c3fc..007ef47 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -268,7 +268,7 @@ gboolean     gs_plugin_offline_update_cancel        (GsPlugin       *plugin,
                                                         GsApp          *app,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
-gboolean        gs_plugin_app_buy                      (GsPlugin       *plugin,
+gboolean        gs_plugin_app_purchase                 (GsPlugin       *plugin,
                                                         GsApp          *app,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
diff --git a/src/gs-shell-details.c b/src/gs-shell-details.c
index 2a9141a..8144dfd 100644
--- a/src/gs-shell-details.c
+++ b/src/gs-shell-details.c
@@ -1177,6 +1177,9 @@ gs_shell_details_filename_to_app_cb (GObject *source,
        g_signal_connect_object (self->app, "notify::licence",
                                 G_CALLBACK (gs_shell_details_notify_state_changed_cb),
                                 self, 0);
+       g_signal_connect_object (self->app, "notify::owned",
+                                G_CALLBACK (gs_shell_details_notify_state_changed_cb),
+                                self, 0);
        g_signal_connect_object (self->app, "notify::progress",
                                 G_CALLBACK (gs_shell_details_progress_changed_cb),
                                 self, 0);
@@ -1310,6 +1313,11 @@ gs_shell_details_app_install_button_cb (GtkWidget *widget, GsShellDetails *self)
        GList *l;
        g_autoptr(GList) addons = NULL;
 
+       if (gs_app_get_price (self->app) > 0 && !gs_app_get_owned (self->app)) {
+               gs_page_purchase_app (GS_PAGE (self), self->app);
+               return;
+       }
+
        /* Mark ticked addons to be installed together with the app */
        addons = gtk_container_get_children (GTK_CONTAINER (self->list_box_addons));
        for (l = addons; l; l = l->next) {
diff --git a/src/gs-utils.c b/src/gs-utils.c
index df5c6de..a8bc56b 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -171,6 +171,11 @@ gs_app_notify_failed_modal (GsApp *app,
                msg = g_strdup_printf (_("Removal of %s failed."),
                                       gs_app_get_name (app));
                break;
+       case GS_PLUGIN_LOADER_ACTION_PURCHASE:
+               /* TRANSLATORS: this is when the purchase fails */
+               msg = g_strdup_printf (_("Purchase of %s failed."),
+                                      gs_app_get_name (app));
+               break;
        default:
                g_assert_not_reached ();
                break;
diff --git a/src/plugins/gs-plugin-dummy.c b/src/plugins/gs-plugin-dummy.c
index 41ceaa3..03a024f 100644
--- a/src/plugins/gs-plugin-dummy.c
+++ b/src/plugins/gs-plugin-dummy.c
@@ -259,15 +259,15 @@ gs_plugin_add_distro_upgrades (GsPlugin *plugin,
 }
 
 /**
- * gs_plugin_app_buy:
+ * gs_plugin_app_purchase:
  */
 gboolean
-gs_plugin_app_buy (GsPlugin *plugin,
-                  GsApp *app,
-                  GCancellable *cancellable,
-                  GError **error)
+gs_plugin_app_purchase (GsPlugin *plugin,
+                       GsApp *app,
+                       GCancellable *cancellable,
+                       GError **error)
 {
-       g_debug ("Buying app");
+       g_debug ("Purchasing app");
        gs_app_set_owned (app, TRUE);
        return TRUE;
 }


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