Unity 8
 All Classes Functions
Payments.cpp
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  */
16 
17 #include "Payments.h"
18 
19 #include <QLocale>
20 
21 #include <libpay/pay-package.h>
22 
23 
24 // The observer callback for the package we are watching.
25 static 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
30 
31  Payments *self = static_cast<Payments*>(user_data);
32 
33  // If the item ID is different, ignore it.
34  if (itemid != self->storeItemId()) {
35  return;
36  }
37 
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  }
47 }
48 
49 Payments::Payments(QObject *parent)
50  : QObject(parent)
51 {
52  m_package = pay_package_new("click-scope");
53  pay_package_item_observer_install(m_package, observer, this);
54 }
55 
56 Payments::~Payments()
57 {
58  pay_package_item_observer_uninstall(m_package, observer, this);
59  pay_package_delete(m_package);
60 }
61 
62 QString Payments::currency() const
63 {
64  return m_currency;
65 }
66 
67 double Payments::price() const
68 {
69  return m_price;
70 }
71 
72 QString Payments::storeItemId() const
73 {
74  return m_store_item_id;
75 }
76 
77 QString Payments::formattedPrice() const
78 {
79  QLocale locale;
80  return locale.toCurrencyString(m_price, m_currency);
81 }
82 
83 void Payments::setCurrency(const QString &currency)
84 {
85  if(m_currency != currency) {
86  m_currency = currency;
87  Q_EMIT currencyChanged(currency);
88  Q_EMIT formattedPriceChanged(formattedPrice());
89  }
90 }
91 
92 void Payments::setPrice(const double price)
93 {
94  if(m_price != price) {
95  m_price = price;
96  Q_EMIT priceChanged(price);
97  Q_EMIT formattedPriceChanged(formattedPrice());
98  }
99 }
100 
101 void Payments::setStoreItemId(const QString &store_item_id)
102 {
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  }
107 
108  if (m_store_item_id.isEmpty()) {
109  return;
110  }
111 
112  pay_package_item_start_verification(m_package, m_store_item_id.toLocal8Bit().data());
113 }
114 
115 void Payments::start()
116 {
117  pay_package_item_start_purchase(m_package, m_store_item_id.toLocal8Bit().data());
118 }