on xenial help sign in
Refactor the pay button a bit to follow pay-service expectations better.
commit7add60a2fb6232947de8d6afc4693913fabc353b authorRodney Dawes <rodney.dawes@canonical.com> dateMon 23 Jun 2014, 16:27 (12 years ago) parentd592b92
2 files changed+43−39
plugins/Ubuntu/Payments/Payments.cpp+37−39 plugins/Ubuntu/Payments/Payments.h+6
plugins/Ubuntu/Payments/Payments.cpp+37−39
@@ -26,9 +26,36 @@
2626
2727#include "Payments.h"
2828
29+
30+// The observer callback for the package we are watching.
31+static void observer(PayPackage* /* package */, const char* /*itemid*/, PayPackageItemStatus status, void* user_data) {
32+ // This function is called in libpay's thread, so be careful what you call
33+ // Emitting signals should be fine as long as they use Queued or Auto
34+ // connections (the default)
35+ // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
36+
37+ Payments *self = static_cast<Payments*>(user_data);
38+
39+ // FIXME: No error reporting from libpay, but we need to show some
40+ // types of errors to the user. https://launchpad.net/bugs/1333403
41+ switch (status) {
42+ case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
43+ Q_EMIT self->purchaseCompleted();
44+ break;
45+ default:
46+ break;
47+ }
48+}
49+
2950Payments::Payments(QObject *parent)
3051 : QObject(parent)
3152{
53+ m_package = pay_package_new("click-scope");
54+}
55+
56+Payments::~Payments()
57+{
58+ pay_package_item_observer_uninstall(m_package, observer, this);
3259}
3360
3461QString Payments::currency() const
@@ -72,52 +99,23 @@ void Payments::setPrice(const double price)
7299
73100void Payments::setStoreItemId(const QString &store_item_id)
74101{
75− if(m_store_item_id != store_item_id){
102+ if (m_store_item_id != store_item_id){
76103 m_store_item_id = store_item_id;
77− Q_EMIT storeItemIdChanged(store_item_id);
104+ Q_EMIT storeItemIdChanged(m_store_item_id);
78105 }
79−}
80−
81−void observer(PayPackage* package, const char* /*itemid*/, PayPackageItemStatus status, void* user_data) {
82− // This function is called in libpay's thread, so be careful with what you call
83− // Emitting signals should be fine as long as they use Queued or Auto connections (the default)
84− // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
85106
86− qDebug() << "observer called";
87− Payments *self = static_cast<Payments*>(user_data);
88− pay_package_item_observer_uninstall(package, observer, self);
89− switch (status) {
90− case PAY_PACKAGE_ITEM_STATUS_VERIFYING:
91− break;
92− case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
93− Q_EMIT self->purchaseCompleted();
94− break;
95− case PAY_PACKAGE_ITEM_STATUS_PURCHASING:
96− break;
97− case PAY_PACKAGE_ITEM_STATUS_NOT_PURCHASED:
98− Q_EMIT self->purchaseError("not purchased");
99− break;
100− case PAY_PACKAGE_ITEM_STATUS_UNKNOWN:
101− break;
102− default:
103− break;
107+ if (m_store_item_id.isEmpty()) {
108+ return;
104109 }
105110
111+ pay_package_item_observer_uninstall(m_package, observer, this);
112+ pay_package_item_observer_install(m_package, observer, this);
113+ pay_package_item_start_verification(m_package,
114+ m_store_item_id.toLocal8Bit().data());
106115}
107116
108117void Payments::start()
109118{
110− qDebug("starting the purchase");
111−
112− auto ba = m_store_item_id.toLocal8Bit();
113− qDebug() << "the item id is" << m_store_item_id;
114− auto package = pay_package_new("clickscope");
115− qDebug() << "after new" << ba.data();
116− pay_package_item_observer_install(package, observer, this);
117− qDebug() << "after observer install";
118− // FIXME: Enable once verification is working in pay-service.
119− //pay_package_item_start_verification(package, ba.data());
120− qDebug() << "after start verify";
121− pay_package_item_start_purchase(package, ba.data());
122− qDebug() << "after start purchase";
119+ pay_package_item_start_purchase(m_package,
120+ m_store_item_id.toLocal8Bit().data());
123121}
plugins/Ubuntu/Payments/Payments.h+6
@@ -25,6 +25,9 @@
2525#include <QObject>
2626#include <QDBusObjectPath>
2727
28+#include <libpay/pay-package.h>
29+
30+
2831class Payments : public QObject
2932{
3033 Q_OBJECT
@@ -36,6 +39,7 @@ class Payments : public QObject
3639
3740public:
3841 explicit Payments(QObject *parent = 0);
42+ virtual ~Payments();
3943
4044 QString currency() const;
4145 double price() const;
@@ -62,6 +66,8 @@ private:
6266 double m_price;
6367 QString m_store_item_id;
6468
69+ PayPackage *m_package;
70+
6571 void startService();
6672};
6773