on cfcde21 help sign in
136 lines 3.5 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "Payments.h"
19#include <QLocale>
21#include <libpay/pay-package.h>
24// The observer callback for the package we are watching.
25static void observer(PayPackage* /* package */, const char* itemid, PayPackageItemStatus status, void* user_data) {
26 // This function is called in libpay's thread, so be careful what you call
27 // Emitting signals should be fine as long as they use Queued or Auto
28 // connections (the default)
29 // http://qt-project.org/doc/qt-5/threads-qobject.html#signals-and-slots-across-threads
31 Payments *self = static_cast<Payments*>(user_data);
33 // If the item ID is different, ignore it.
34 if (itemid != self->storeItemId()) {
35 return;
36 }
38 // FIXME: No error reporting from libpay, but we need to show some
39 // types of errors to the user. https://launchpad.net/bugs/1333403
40 switch (status) {
41 case PAY_PACKAGE_ITEM_STATUS_PURCHASED:
42 Q_EMIT self->purchaseCompleted();
43 break;
44 case PAY_PACKAGE_ITEM_STATUS_PURCHASING:
45 self->setPurchasing(true);
46 break;
47 case PAY_PACKAGE_ITEM_STATUS_NOT_PURCHASED:
48 if (self->purchasing()) {
49 self->setPurchasing(false);
50 Q_EMIT self->purchaseCancelled();
51 }
52 break;
53 default:
54 break;
55 }
58Payments::Payments(QObject *parent)
59 : QObject(parent),
60 m_purchasing(false)
62 m_package = pay_package_new("click-scope");
63 pay_package_item_observer_install(m_package, observer, this);
66Payments::~Payments()
68 pay_package_item_observer_uninstall(m_package, observer, this);
69 pay_package_delete(m_package);
72QString Payments::currency() const
74 return m_currency;
77double Payments::price() const
79 return m_price;
82QString Payments::storeItemId() const
84 return m_store_item_id;
87QString Payments::formattedPrice() const
89 QLocale locale;
90 return locale.toCurrencyString(m_price, m_currency);
93bool Payments::purchasing() const
95 return m_purchasing;
98void Payments::setCurrency(const QString &currency)
100 if(m_currency != currency) {
101 m_currency = currency;
102 Q_EMIT currencyChanged(currency);
103 Q_EMIT formattedPriceChanged(formattedPrice());
104 }
107void Payments::setPrice(const double price)
109 if(m_price != price) {
110 m_price = price;
111 Q_EMIT priceChanged(price);
112 Q_EMIT formattedPriceChanged(formattedPrice());
113 }
116void Payments::setStoreItemId(const QString &store_item_id)
118 if (m_store_item_id != store_item_id) {
119 m_store_item_id = store_item_id;
120 Q_EMIT storeItemIdChanged(m_store_item_id);
121 }
123 if (m_store_item_id.isEmpty()) {
124 return;
125 }
128void Payments::setPurchasing(bool is_purchasing)
130 m_purchasing = is_purchasing;
133void Payments::start()
135 pay_package_item_start_purchase(m_package, m_store_item_id.toLocal8Bit().data());