on 0dc3bc4 help sign in
118 lines 3.1 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 default:
45 break;
46 }
49Payments::Payments(QObject *parent)
50 : QObject(parent)
52 m_package = pay_package_new("click-scope");
53 pay_package_item_observer_install(m_package, observer, this);
56Payments::~Payments()
58 pay_package_item_observer_uninstall(m_package, observer, this);
59 pay_package_delete(m_package);
62QString Payments::currency() const
64 return m_currency;
67double Payments::price() const
69 return m_price;
72QString Payments::storeItemId() const
74 return m_store_item_id;
77QString Payments::formattedPrice() const
79 QLocale locale;
80 return locale.toCurrencyString(m_price, m_currency);
83void Payments::setCurrency(const QString &currency)
85 if(m_currency != currency) {
86 m_currency = currency;
87 Q_EMIT currencyChanged(currency);
88 Q_EMIT formattedPriceChanged(formattedPrice());
89 }
92void Payments::setPrice(const double price)
94 if(m_price != price) {
95 m_price = price;
96 Q_EMIT priceChanged(price);
97 Q_EMIT formattedPriceChanged(formattedPrice());
98 }
101void Payments::setStoreItemId(const QString &store_item_id)
103 if (m_store_item_id != store_item_id) {
104 m_store_item_id = store_item_id;
105 Q_EMIT storeItemIdChanged(m_store_item_id);
106 }
108 if (m_store_item_id.isEmpty()) {
109 return;
110 }
112 pay_package_item_start_verification(m_package, m_store_item_id.toLocal8Bit().data());
115void Payments::start()
117 pay_package_item_start_purchase(m_package, m_store_item_id.toLocal8Bit().data());